Description.
Write a function named first_non_repeating_letter that takes a string input, and returns the first character that is not repeated anywhere in the string.
For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the string.
As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS' should return 'T'.
If a string contains all repeating characters, it should return an empty string ("") or None -- see sample tests.
string을 받는 first_non_repeating_letter라는 함수를 만들고 반복되지 않는 첫번째 string값을 반환해라
예시를 들어, 'stress'라는 값을 입력받을 경우, 대소문자 관계 없이 검사하지만, 반환시에는 대소문자를 구분한다.
'sTreSS'를 입력 받는 경우 반환값은 'T'가 된다.
만약, 입력 받은 문자열이 모두 반복문자일 경우, 빈 string("") 또는 None을 반환한다.
Example (with Result).
firstNonRepeatingLetter('a')// 'a'
firstNonRepeatingLetter('stress') // 't'
firstNonRepeatingLetter('moonmen') // 'e'
Solution 1.
function firstNonRepeatingLetter(s) {
const text = s.toLowerCase();
for(let i=0; i < text.length; i++) {
if(text.indexOf(text[i]) === text.lastIndexOf(text[i])) return s[i];
}
return "";
}
Solution 2.
function firstNonRepeatingLetter(s) {
return s[s.toLowerCase().split('').findIndex(letter => s.toLowerCase().split('').filter(l => l === letter).length === 1)] || '';
}
'1Day 1Practice' 카테고리의 다른 글
[codewars] Count characters in your string (0) | 2022.07.08 |
---|---|
[codewars] Human Readable Time (0) | 2022.07.07 |
[codewars] Valid Parentheses (0) | 2022.06.15 |
[codewars] Stop gninnips My sdroW! (0) | 2022.06.14 |
[codewars] Isograms (0) | 2022.06.10 |