Description
- An isogram is a word that has no repeating letters, consecutive or non-consecutive.
- Assume the empty string is an isogram.
- Ignore letter case.
Solution 01
function isIsogram(str) {
str = str.toLowerCase();
for (let i = 0; i < str.length - 1; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str[i] === str[j]) {
return false;
}
}
}
return true;
}
isIsogram(''); // true
isIsogram('abc'); // true
isIsogram('aab'); // false
isIsogram('aba'); // false
isIsogram('Aba'); // false
isIsogram('Isogram'); // true
isIsogram('isIsogram'); // false
isIsogram('Codewars'); // true
Solution 02
function isIsogram(str) {
const arr = str.toLowerCase().split('').sort();
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] === arr[i + 1]) {
return false;
}
}
return true;
}
isIsogram(''); // true
isIsogram('abc'); // true
isIsogram('aab'); // false
isIsogram('aba'); // false
isIsogram('Aba'); // false
isIsogram('Isogram'); // true
isIsogram('isIsogram'); // false
isIsogram('Codewars'); // true
Solution 03
function isIsogram(str) {
str = str.toLowerCase();
for (let i = 0; i < str.length; i++) {
if (str.indexOf(str.charAt(i), i + 1) !== -1) {
return false;
}
}
return true;
}
isIsogram(''); // true
isIsogram('abc'); // true
isIsogram('aab'); // false
isIsogram('aba'); // false
isIsogram('Aba'); // false
isIsogram('Isogram'); // true
isIsogram('isIsogram'); // false
isIsogram('Codewars'); // true
Solution 04
function isIsogram(str) {
return !str.match(/([a-z]).*\1/i);
}
isIsogram(''); // true
isIsogram('abc'); // true
isIsogram('aab'); // false
isIsogram('aba'); // false
isIsogram('Aba'); // false
isIsogram('Isogram'); // true
isIsogram('isIsogram'); // false
isIsogram('Codewars'); // true