works because if there are as many distinct characters as the length, there can't be duplicates
const solve = str => { const len = str.length let v = str.charCodeAt() for (let i = 1; i < len; i++) if (str.charCodeAt(i) < v) v = str.charCodeAt(i) for (let i = 0; i < len; i++) if (!str.includes(String.fromCharCode(v++))) return false return true }
const enc = new TextEncoder('ascii') const solve = str => { const buf = enc.encode(str) const len = buf.length buf.sort() let v = buf[0] for (let i = 1; i < len; i++) if (buf[i] !== ++v) return false return true }
const enc = new TextEncoder('ascii') const solve = str => { const buf = enc.encode(str) const len = buf.length let v = buf.reduce(Math.min) for (let i = 0; i < len; i++) (buf.includes(v++)) return false return true }
const enc = new TextEncoder('ascii') const solve = str => { const buf = enc.encode(str) buf.sort() return !!buf.reduce((a, x) => x - a === 1 ? x : 0) }
const solve = str => !!new TextEncoder('ascii') .encode(str) .sort() .reduce((a, x) => x - a === 1 ? x : 0)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript
Efficient (for small strings)
works because if there are as many distinct characters as the length, there can't be duplicates
TextEncoder
TextEncoder but without sorting
TextEncoder without mutation
TextEncoder but finally a oneliner