isPangram.js

Codewars 알고리즘 풀이


Problem

  • A pangram is a sentence that contains every single letter of the alphabet at least once.
    • pangram은 알파벳이 모든 글자를 적어도 한 번 포함하는 문장이다.
  • For example, the sentence “The quick brown fox jumps over the lazy dog” is a pangram, because it uses the letters A-Z at least once.
    • 예를 들어 “The quick brown fox jumps over the lazy dog” 문장은 A-Z를 적어도 한 번 이상 사용하므로 pangram이다.
  • Given a string, detect whether or not it is a pangram.
    • 주어진 문자열이 pangram인지를 판단한다.
  • Ignore numbers and punctuation.
    • 숫자와 구두점은 무시한다.



Solution 01

function isPangram(str) {
  str = str.toLowerCase();
  const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  
  for (let i = 0; i < alphabet.length; i++) {
    if (str.indexOf(alphabet[i]) === -1) {
      return false;
    }
  }
  return true;
}

isPangram('The quick brown fox jumps over the lazy dog');
isPangram('Waltz, nymph, for quick jigs vex Bux.');
isPangram('Watch "Jeopardy!", Alex Trebek\'s fun TV quiz game.');  // true


Solution 02

function isPangram(str) {
  str = str.toLowerCase();
  const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  
  for (let i = 0; i < 26; i++) {
    if (str.indexOf(alphabet.charAt(i)) === -1) {
      return false;
    }
  }
  return true;
}

isPangram('The quick brown fox jumps over the lazy dog');
isPangram('Waltz, nymph, for quick jigs vex Bux.');
isPangram('Watch "Jeopardy!", Alex Trebek\'s fun TV quiz game.');  // true


Solution 03

function isPangram(str) {
  str = str.toLowerCase();
  const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  
  return alphabet.split('').every(i => {
    return str.indexOf(i) !== -1;
  });
}

isPangram('The quick brown fox jumps over the lazy dog');
isPangram('Waltz, nymph, for quick jigs vex Bux.');
isPangram('Watch "Jeopardy!", Alex Trebek\'s fun TV quiz game.');  // true


Solution 04

function isPangram(str) {
  str = str.toLowerCase();
  const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  
  return alphabet.split('').every(i => str.includes(i));
}

isPangram('The quick brown fox jumps over the lazy dog');
isPangram('Waltz, nymph, for quick jigs vex Bux.');
isPangram('Watch "Jeopardy!", Alex Trebek\'s fun TV quiz game.');  // true


Solution 05

function isPangram(str) {
  return (str.match(/([a-z])(?!.*\1)/gi) || []).length === 26;
}

isPangram('The quick brown fox jumps over the lazy dog');
isPangram('Waltz, nymph, for quick jigs vex Bux.');
isPangram('Watch "Jeopardy!", Alex Trebek\'s fun TV quiz game.');  // true