코테연습
6.Isograms
hxunz
2022. 3. 16. 20:54
Description:
An isogram is a word that has no repeating letters, consecutive or non-consecutive. 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.
Example: (Input --> Output)
"Dermatoglyphics" --> true
"aba" --> false
"moOse" --> false (ignore letter case)
function isIsogram(str){
if (str.length === 0) {
return true;
}
const words = [...str.toLowerCase()];
for (let i = 0; i < words.length - 1; i++) {
for (let j = i + 1; j < words.length; j++) {
if (words[i] === words[j]) {
return false;
}
}
}
return true;
}
주어진 string이 없으면 true를 리턴하는 예외처리를 먼저 해주고
주어진 string을 소문자로 바꾸고 알파벳 하나씩 잘라서 배열에 넣어줬다.
toLowerCase()를 하면 대문자를 소문자로 변경해주고
[...]이렇게 해주니까 단어를 알파벳 하나씩 끊어서 배열로 저장되도록(?) 해주더라,,
for문을 두번 실행하면서 비교해야되는 알파벳이랑 비교 당하는 알파벳을 서로 비교해서 같으면 false를 리턴하도록 했고
for문을 다 돌았는데도 (알파벳이 겹치는 상황이 없을 경우) true 를 리턴하도록 했다.