Notice
Recent Posts
Recent Comments
Link
.Zzumbong
[leetCode/JS] 1704. Determine if String Halves Are Alike ๋ณธ๋ฌธ
coding test/leetCode
[leetCode/JS] 1704. Determine if String Halves Are Alike
์ญ๋ด 2022. 12. 1. 13:43๋์ด๋ [ ๐ ] Easy
๋ฌธ์ ์ค๋ช
You are given a string s
of even length. Split this string into two halves of equal lengths, and let a
be the first half and b
be the second half.
Two strings are alike if they have the same number of vowels ('a'
,'e'
, 'i'
, 'o'
, 'u'
, 'A'
, 'E'
, 'I'
, 'O'
, 'U'
). Notice that s
contains uppercase and lowercase letters.
Return true
if a
and b
are alike. Otherwise, return false
.
์ง์ ๊ธธ์ด string s๋ฅผ ๋ฐ ๋๋ ์ ํฌํจ๋ ๋ชจ์์ ๊ฐฏ์๊ฐ ๋๊ฐ์ผ๋ฉด true.
์ ์ถ๋ ฅ ์
Example 1:
Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
Example 2:
Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.
Constraints
2 <= s.length <= 1000
s.length
is even.s
consists of uppercase and lowercase letters.
๋ด ์๋ฃจ์
- ๋ฌธ์์ด์ 2๊ฐ๋ก ๋๋๋ค๊ธฐ๋ณด๋จ ์ค๊ฐ index๋ฅผ ๊ธฐ์ค์ผ๋ก
- i๊ฐ ์ค๊ฐ index ๋ณด๋ค ์์ ๋ -1, ํด ๋ +1์ ํ์ฌ์ count๋ฅผ ๊ตฌํ๋ค.
- ๋ง์ฝ count๊ฐ 0์ด๋๋ฉด ๋ชจ์์ ์ซ์๊ฐ ๋๊ฐ๊ธฐ ๋๋ฌธ์ true๊ฐ ๋๋ค.
/**
* @param {string} s
* @return {boolean}
*/
var halvesAreAlike = function(s) {
const vowels = ['a', 'e', 'i', 'o', 'u']
s = s.toLowerCase();
let count = 0;
for(let i = 0; i < s.length; i++) {
if(vowels.includes(s[i])) {
count = count + (i < s.length/2 ? -1 : +1)
}
}
return count === 0;
};
๊ฐ์ํ
- ๋๋ฆ ๋น ๋ฅธํธ!
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 451. Sort Characters By Frequency (0) | 2022.12.03 |
---|---|
[leetCode/JS] 1657. Determine if Two Strings Are Close (0) | 2022.12.02 |
[leetCode/JS] 1207. Unique Number of Occurrences (0) | 2022.11.30 |
[leetCode/JS] 380. Insert Delete GetRandom O(1) (0) | 2022.11.29 |
[leetCode/JS] 2225. Find Players With Zero or One Losses (0) | 2022.11.28 |
0 Comments