7kyu
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.
中翻:字串中一個字母只出現一次(不分大小寫),若符合條件則回傳true(空字串也視為Isogram)。
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
}
}
好像有點冗長,後來查到一位的寫法比我精簡許多(查看下方),但想法是相同的。
概念是:
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;
}
author: stellartux
export function isIsogram (str: string): boolean {
return (new Set(str.toLowerCase())).size === str.length
}
超級精簡的code~~ 十分佩服
array.split()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
String.prototype.toLowerCase()
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
Set.prototype.size
https://www.fooish.com/javascript/ES6/Set-and-WeakSet.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/size