As I understand that code, sets only accept unique characters, and since he cleaned out the hyphens and spaces if the len of the set(cleaned) isn't the len of cleaned that means we have a duplicate character, hence it's not an isogram. 🤔
sets
hyphens
spaces
len
set(cleaned)
cleaned
Please correct me if I'm wrong. 🙂
Yes, you are right!
Fun thing, the same code works in Javascript too with some small modifications.
function is_isogram (str) { const cleaned = str.replace('-', '').replace(' ', '').toLowerCase(); return new Set(cleaned).size == cleaned.length; } console.log(is_isogram("lumberjacks")) // Returns true console.log(is_isogram("background")) // Returns true console.log(is_isogram("downstream")) // Returns true console.log(is_isogram("six-year-old")) // Returns true console.log(is_isogram("isograms")) // Returns false console.log(is_isogram("\u0041\u0042\u0043")) // Returns true console.log(is_isogram("\u0041\u0061\u0042\u0043")) // Returns false
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.
As I understand that code,
sets
only accept unique characters, and since he cleaned out thehyphens
andspaces
if thelen
of theset(cleaned)
isn't thelen
ofcleaned
that means we have a duplicate character, hence it's not an isogram. 🤔Please correct me if I'm wrong. 🙂
Yes, you are right!
Fun thing, the same code works in Javascript too with some small modifications.