Skip to content

Commit 20c11e6

Browse files
committed
스트링 단어 개수
1 parent 636473a commit 20c11e6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

NumOfBalloons.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
public class NumOfBalloons {
2+
public static void main(String[] args) {
3+
System.out.println(maxNumberOfBalloons("nlaebolko"));
4+
}
5+
6+
// my solution
7+
public static int maxNumberOfBalloons(String text) {
8+
StringBuffer str = new StringBuffer(text);
9+
int len = text.length();
10+
int i = 0, min = (int)Double.POSITIVE_INFINITY ,idx = 0;
11+
int[] arr = {0, 0, 0, 0, 0}; //[b, a, l, o, n]
12+
13+
while (len > 0) {
14+
if (str.charAt(i) == 'b')
15+
arr[0] += 1;
16+
if (str.charAt(i) == 'a')
17+
arr[1] += 1;
18+
if (str.charAt(i) == 'l')
19+
arr[2] += 1;
20+
if (str.charAt(i) == 'o')
21+
arr[3] += 1;
22+
if (str.charAt(i) == 'n')
23+
arr[4] += 1;
24+
len--;
25+
i++;
26+
}
27+
arr[0] *= 2;
28+
arr[1] *= 2;
29+
arr[4] *= 2;
30+
31+
for(i = 0; i<arr.length; i++){
32+
if(min > arr[i]) {
33+
min = arr[i];
34+
idx = i;
35+
}
36+
}
37+
38+
if(arr[2] == min || arr[3]==min){
39+
if(arr[idx]%2 == 0)
40+
return arr[idx]/2;
41+
else
42+
return (arr[idx]-1)/2;
43+
}
44+
else
45+
return arr[idx]/2;
46+
47+
}
48+
49+
/*
50+
//best solution
51+
public static int maxNumberOfBalloons2(String text) {
52+
int result = 0;
53+
char count[]=new char[26];
54+
for(int i=0;i<text.length();i++) count[text.charAt(i)-'a']++;
55+
56+
String res="balloon";
57+
while(true){
58+
for(int i=0;i<res.length();i++) if (count[res.charAt(i) - 'a']— <= 0) return result;
59+
result++;
60+
}
61+
}
62+
*/
63+
}

0 commit comments

Comments
 (0)