Skip to content

Commit ced0fe0

Browse files
Merge pull request #1709 from CodingTestStudy2/이진희
[이진희] Day28 (++ Day26)
2 parents a012ddb + d23e153 commit ced0fe0

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
1. 아이디어 : 숫자의 빈도수 계산 후, 가장 빈도수가 적은 숫자 반환
4+
정답이 여러개일 경우 더 작은 숫자 반환
5+
각 숫자를 카운팅하여 계산 후 해결
6+
7+
2. 시간복잡도 : O(31 + 10) => O(1)
8+
9+
3. 자료구조/알고리즘 : 배열
10+
11+
*/
12+
13+
class Solution {
14+
public int getLeastFrequentDigit(int n) {
15+
int[] nums = new int[10];
16+
17+
while(n > 0) {
18+
int num = n%10;
19+
nums[num]++;
20+
21+
n/=10;
22+
}
23+
24+
int minNum = 0;
25+
int minCnt = 100;
26+
for(int i=0; i<10; i++) {
27+
if(nums[i] == 0 || minCnt<=nums[i]) continue;
28+
29+
minNum = i;
30+
minCnt = nums[i];
31+
}
32+
33+
return minNum;
34+
}
35+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
3+
1. 아이디어 : 조건에 맞춰 구현
4+
리스트를 사용하여 각 문자열을 압축, 각각의 개수 구함
5+
words 배열을 완전탐색하여 똑같이 계산 후, 늘릴수 있는지, 조건에 맞춰 하나하나 비교
6+
7+
2. 시간복잡도 : O(S+N*M) (N = words.length, M = words 배열의 각 단어의 길이)
8+
9+
3. 자료구조/알고리즘 : 구현
10+
11+
*/
12+
13+
class Solution {
14+
private List<int[]> ori;
15+
16+
public int expressiveWords(String s, String[] words) {
17+
ori = new ArrayList<>();
18+
char memo = s.charAt(0);
19+
int cnt = 1;
20+
int ans = 0;
21+
22+
for(int i=1; i<s.length(); i++) {
23+
char c = s.charAt(i);
24+
if(c != memo) {
25+
ori.add(new int[]{memo, cnt});
26+
memo = c;
27+
cnt = 1;
28+
}
29+
else cnt++;
30+
}
31+
ori.add(new int[]{memo, cnt});
32+
33+
for(String word : words) {
34+
if(checkExpressiveWords(word)) ans++;
35+
}
36+
37+
return ans;
38+
}
39+
40+
private boolean checkExpressiveWords(String s) {
41+
List<int[]> tmp = new ArrayList<>();
42+
char memo = s.charAt(0);
43+
int cnt = 1;
44+
int ans = 0;
45+
46+
for(int i=1; i<s.length(); i++) {
47+
char c = s.charAt(i);
48+
if(c != memo) {
49+
tmp.add(new int[]{memo, cnt});
50+
memo = c;
51+
cnt = 1;
52+
}
53+
else cnt++;
54+
}
55+
tmp.add(new int[]{memo, cnt});
56+
57+
if(tmp.size() != ori.size()) return false;
58+
for(int i=0; i<tmp.size(); i++) {
59+
int[] o = ori.get(i);
60+
int[] t = tmp.get(i);
61+
62+
if(o[0] != t[0]) return false;
63+
if(o[1] < t[1]) return false;
64+
if(o[1] != t[1] && o[1] < 3) return false;
65+
}
66+
67+
return true;
68+
}
69+
}

0 commit comments

Comments
 (0)