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
- Tree
- implement
- 코딩테스트
- 구현
- 파이썬
- 코테
- SQL
- sorting
- Math
- Binary Tree
- java
- dynamic programming
- string
- Matrix
- hash table
- 자바
- Data Structure
- Counting
- bit manipulation
- array
- geometry
- two pointers
- Number Theory
- Method
- simulation
- Class
- Binary Search
- database
- Stack
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 409. Longest Palindrome 본문
1. Input
1) String s
2. Output
1) s의 문자들로 만들 수 있는 palindrome 중 가장 긴 것의 길이를 반환
// palindrome은 앞에서 부터 읽으나 뒤에서 부터 읽으나 같은 문자열을 뜻한다.
3. Constraint
1) 1 <= s.length <= 2000
2) s는 영어 대소문자 중 몇개로 이루어져 있다.
3) palindrome은 대소문자를 구분한다.
4. Example
Input: s = "abccccdd" -> Output: 7 (dccaccd)
Input: s = "a" -> Output: 1 (a)
5. Code
1) 첫 코드(2023/02/19)
HashMap<Character,Integer> m = new HashMap<Character,Integer>();
for(int i=0 ; i<s.length() ; i++){
if(m.containsKey(s.charAt(i))) m.put(s.charAt(i), m.get(s.charAt(i))+1);
else m.put(s.charAt(i), 1);
}
int answer = 0;
boolean odd = false;
Iterator it = m.entrySet().iterator();
while(it.hasNext()){
int i = (int)((Map.Entry)it.next()).getValue();
if(i%2==0) answer+=i;
else {
odd = true;
answer+=i-1;
}
}
return odd ? answer+1 : answer;
- odd=true로 하는 것을 한번만 실행하게 바꾸고 싶었는데, 이렇다할 방법이 떠오르지 않았다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Medium] 438. Find All Anagrams in a String (0) | 2023.02.21 |
---|---|
[프로그래머스/Lv.1] 카드 뭉치 (0) | 2023.02.20 |
[LeetCode/Medium] 398. Random Pick Index (0) | 2023.02.16 |
[LeetCode/Medium] 384. Shuffle an Array (0) | 2023.02.15 |
[프로그래머스/Lv.1] 가장 가까운 같은 글자 (0) | 2023.02.14 |