forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtilGold.java
More file actions
70 lines (60 loc) · 1.6 KB
/
StringUtilGold.java
File metadata and controls
70 lines (60 loc) · 1.6 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class StringUtilGold
{
public static void main(String[] args)
{
String[] test = {"zebra", "ant", "car", "boat", "alien", "aardvark", "giraffe", "owl", "earth" };
for(int i=0;i<test.length;i++)
{
System.out.println(sort(test)[i]);
}
String[] str = {"dog", "dog"};
System.out.println(sameCount(str));
}
private static boolean sameCount(String[] str)
{
int count = 0;
int prevCount = 0;
String prevString = "";
boolean[] doIt = new boolean[str.length];
for(int i=0;i<str.length;i++)
{
doIt[i]=true;
}
for(int i=0;i<str.length;i++)
{
count=0;
if(doIt[i])
{
for (int j = i; j < str.length; j++)
{
if (str[j].equals(str[i]))
{
count++;
doIt[j] = false;
}
}
if (count != prevCount && i > 0)
return false;
prevCount = count;
}
}
return true;
}
private static String[] sort(String[] str)
{
String holder;
for(int i=0;i<str.length;i++)
{
for (int j = i+1; j < str.length; j++)
{
if(str[i].compareTo(str[j])>0)
{
holder=str[i];
str[i]=str[j];
str[j]=holder;
}
}
}
return str;
}
}