1

Isogram

原始題目isogram的解釋

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.

``````isIsogram "Dermatoglyphics" == true
isIsogram "aba" == false
isIsogram "moOse" == false -- ignore letter case
``````

自解:

``````export function isIsogram(str: string){
let lowerCaseStr = str.toLowerCase();
let arr = lowerCaseStr.split("");
let setArrNumber = new Set(arr).size
if(setArrNumber !== arr.length){
return false
}
else{
return true
}
}
``````

1. 先將字串全部轉成lowerCase
2. 將字串的每個字母拆解
3. 去比對字母是否有重複，透過new Set()可以讓所有的值都是唯一的，若有值重複則會被忽略，
所以去比較Set完後的元素數量(透過set.prototype.size)是否和原本拆解的字母陣列數量相同，
若不同則代表有重複字母，回傳false，反之則回傳true

他解1:

`author: fullstackdevguy`

``````export function isIsogram(str: string){
//resolve str to lowercase first
str = str.toLowerCase();
//transform string to a Set to remove duplicates and
//compare the size of the set to the length of the str
return new Set(str.split('')).size === str.length;
}
``````

他解2:

`author: stellartux`

``````export function isIsogram (str: string): boolean {
return (new Set(str.toLowerCase())).size === str.length
}
``````

此次題目使用到的方法和參考網址:

String.prototype.toLowerCase()
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase