3

I need to check if a word is Isogram, the meaning of Isogram, is that a word has no repeating letters.

I need to implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.

Here is a test case

isIsogram( "Dermatoglyphics" ) == true
isIsogram( "aba" ) == false
isIsogram( "moOse" ) == false // -- ignore letter case

I am thinking on do this with a Regex.

function isIsogram(str){
  //...
}

can you help?

Non
  • 8,409
  • 20
  • 71
  • 123

3 Answers3

12

as simple as that

function isIsogram (str) {

    return !/(\w).*\1/i.test(str);
}
orvi
  • 3,142
  • 1
  • 23
  • 36
4

This will do:

function isIsogram (str) {
    return !/(.).*\1/.test(str);
}
thedarklord47
  • 3,183
  • 3
  • 26
  • 55
  • Sorry for ressurecting and old submission, can you walk through how the syntax is matching the string. The readability is a little confusing – Kode_12 Aug 20 '16 at 20:28
  • `(.)` capture any letter. `.*` followed by any sequence of letters including nothing. `\1` followed by the first letter we captured. In other words, this will match any string that has at least two of the same letters. Take the opposite of that boolean. – thedarklord47 Aug 22 '16 at 18:44
2

You can use it like this by converting the input to a lower case:

var re = /^(?:([a-z])(?!.*\1))*$/;

function isIsogram(str) {
   return re.test( str.toLowerCase() );
}

Testing:

isIsogram("Dermatoglyphics")
true
re.test("aba")
false
isIsogram("moOse")
false
anubhava
  • 761,203
  • 64
  • 569
  • 643