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 |
Tags
- Binary Search
- Class
- Data Structure
- Binary Tree
- 자바
- simulation
- Stack
- sorting
- java
- dynamic programming
- SQL
- implement
- greedy
- string
- Tree
- database
- array
- two pointers
- 파이썬
- Method
- 코테
- 구현
- Counting
- Matrix
- hash table
- Number Theory
- 코딩테스트
- bit manipulation
- geometry
- Math
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2399. Check Distances Between Same Letters 본문
1. Input
1) String s
2) int[] distance
2. Output
1) s에 있는 같은 문자의 거리가 distance에 제시된 것과 같다면 true, 다르면 false를 반환
- a는 0, b는 1, ..., z는 25로 매칭된다.
3. Constraint
1) 2 <= s.length <= 52
2) s는 영어 소문자로 이루어져 있다.
3) s에 있는 각 문자는 2번씩 등장한다.
4) distance.length == 26
5) 0 <= distance[i] <= 50
4. Example
Input: s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] -> Output: true
설명:
- a의 위치는 0, 2이고 distance[0] = 1이므로 ok
- b의 위치는 1, 5이고 distance[1] = 3이므로 ok
- c의 위치는 3, 4이고 distance[2] = 0이므로 ok
- distance[3] = 5지만 d는 s에 없으므로 무시한다.
5. Code
1) 첫 코드(2023/05/03)
class Solution {
public boolean checkDistances(String s, int[] distance) {
boolean answer = true;
HashMap<Character,Integer> m = new HashMap<>();
for(int i=0 ; i<s.length() ; i++){
char c = s.charAt(i);
if(!m.containsKey(c))
m.put(c,i);
else
m.put(c, i-m.get(c)-1);
}
for(char c : m.keySet())
if(m.get(c)!=distance[c-'a']){
answer = false; break;
}
return answer;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2418. Sort the People (0) | 2023.05.03 |
---|---|
[LeetCode/Easy] 2413. Smallest Even Multiple (0) | 2023.05.03 |
[LeetCode/Easy] 2395. Find Subarrays With Equal Sum (0) | 2023.05.03 |
[백준 온라인 저지] 13909. 창문 닫기 (0) | 2023.05.02 |
[백준 온라인 저지] 4134. 다음 소수 (0) | 2023.05.02 |