Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Data Structure
- Number Theory
- geometry
- java
- 파이썬
- greedy
- implement
- Method
- Binary Search
- 구현
- 코테
- two pointers
- database
- Math
- Binary Tree
- Matrix
- 자바
- hash table
- Tree
- simulation
- bit manipulation
- array
- SQL
- Stack
- sorting
- 코딩테스트
- dynamic programming
- Class
- string
- Counting
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1704. Determine if String Halves Are Alike 본문
1. Input
1) 문자열 s
2. Output
1) s를 반으로 나눴을 때, 나눈 두 문자열이 alike하면 true를, 다르면 false를 반환
- 문자열이 alike하다는 것은 각 문자열에 있는 모음의 수가 같다는 것을 의미한다.
3. Constraint
1) 2 <= s.length <= 1000
2) s.length%2==0
3) s는 영어 대소문자로 이루어져 있다.
4. Example
Input: s = "book" -> Output: true (bo / ok)
Input: s = "textbook" -> Output: false (text / book)
5. Code
1) 첫 코드(2022/06/10)
s = s.toLowerCase();
int count = 0;
// a의 모음 개수 세기
for(int i=0 ; i<s.length()/2 ; i++)
if(s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u')
count++;
// b의 모음 개수 세기
for(int i=s.length()/2 ; i<s.length() ; i++)
if(s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u')
count--;
return count==0 ? true : false;
2) 길이를 줄여본 코드(2023/01/02)
s = s.toLowerCase();
return s.substring(0, s.length()/2).replaceAll("[aeiou]","").length()
== s.substring(s.length()/2, s.length()).replaceAll("[aeiou]","").length();
- 실행시간은 차이가 없고, 메모리는 더 먹었다. 문자열 연산을 안하는 방향으로 가는게 맞는듯하다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1716. Calculate Money in Leetcode Bank (0) | 2023.01.02 |
---|---|
[LeetCode/Easy] 976. Largest Perimeter Triangle (0) | 2023.01.02 |
[LeetCode/Easy] 1688. Count of Matches in Tournament (0) | 2023.01.02 |
[LeetCode/Easy] 1684. Count the Number of Consistent Strings (0) | 2023.01.02 |
[LeetCode/Easy] 1678. Goal Parser Interpretation (0) | 2023.01.02 |