Skip to content

Commit a9ac9ec

Browse files
committed
[이진희]3545. Minimum Deletions for At Most K Distinct Characters
1 parent c05fe9a commit a9ac9ec

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
1. 아이디어 : k개만큼 서로 다른 문자열을 남긴채 삭제하는 최소 알파벳 개수를 구한다.
4+
5+
2. 시간복잡도 : O(N)+O(26)+O(26log26)+O(26) => O(N) N: s 문자열의 길이
6+
7+
3. 자료구조/알고리즘 : 문자열 카운팅
8+
9+
*/
10+
11+
class Solution {
12+
public int minDeletion(String s, int k) {
13+
int[] word = new int[26];
14+
int distinct = 0;
15+
int ans = 0;
16+
for(int i=0; i<s.length(); i++) word[s.charAt(i)-'a']++;
17+
18+
for(int i=0; i<26; i++) {
19+
if(word[i] != 0) distinct++;
20+
}
21+
22+
Arrays.sort(word);
23+
24+
for(int i=0; i<26; i++) {
25+
if(distinct <= k) break;
26+
if(word[i]==0) continue;
27+
28+
ans+=word[i];
29+
distinct--;
30+
}
31+
32+
return ans;
33+
}
34+
}

0 commit comments

Comments
 (0)