isIsogram.js (7kyu 10)

Codewars 알고리즘 풀이


Description

  • An isogram is a word that has no repeating letters, consecutive or non-consecutive.
    • isogram은 반복 글자가 없는 단어이다.
  • Assume the empty string is an isogram.
    • 빈 문자열은 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