export function isIsogram(word) {
return !/([a-z]).*?\1/i.test(word);
}
- This solution uses a regular expression (also known as "regex')
test
method to see if there is a duplicated letter. - The
[a-z]
character class looks for anya
-z
letter. - The parentheses "capture" the letter and remember it in a capturing group.
In the next part of the pattern .*?
:
-
.
looks for another character which is not a line terminator. -
*
looks for zero or more of those characters. -
?
looks for as few of those characters as possible, until
\1
finds a repeat of the first captured character.
If no repeat is found for the first captured character,
then the regex tries again and matches the next a
-z
as the first captured character, and so on.
The i
at the end of the pattern means to ignore case when matching, so [a-z]
will also find [A-Z]
.
The !
at the beginning of the pattern is the logical NOT operator.
The function returns true
if the regex does not find a repeated letter.
6th Nov 2024
·
Found it useful?