-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord.java
More file actions
173 lines (150 loc) · 3.02 KB
/
Copy pathWord.java
File metadata and controls
173 lines (150 loc) · 3.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//package anagrams.algs;
/**
*
* @author Ben Zifkin
*/
public class Word {
String value;
String key;
public Word(String value) {
if(value!=null){
this.value = value;
key = sorted(value);
}
}
public String sorted(String value) {
char[] c = value.toCharArray();
sort(c);
return new String(c);
}
//helper method to hide the implementation details
public static void sort(char[] c) {
quicksort(c, 0, c.length - 1);
}
// quicksort the word into lexographic order
private static void quicksort(char[] arr, int start, int end) {
if (start <= end) {
int pivot = choosePivot(arr, start, end);
pivot = partition(arr, start, end, pivot);
quicksort(arr, start, pivot - 1);
quicksort(arr, pivot + 1, end);
}
}
// gives a random pivot, ensuring the worst case runtime is O(n log n).
private static int choosePivot(char[] arr, int start, int end) {
return (int) (Math.random() * (end - start + 1)) + start;
}
private static int partition(char[] arr, int start, int end, int pivot) {
char pivotValue = arr[pivot];
swap(arr, pivot, end);
int storeIndex = start;
for (int i = start; i < end; i++) {
if (arr[i] <= pivotValue) {
swap(arr, storeIndex, i);
storeIndex++;
}
}
swap(arr, storeIndex, end);
return storeIndex;
}
private static void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
//junk from earlier attempts
/*
public int makeHashKey(String value) {
int temp = 1;
for (int i = 0; i <= value.length() - 1; i++) {
char ch = value.charAt(i);
switch (ch) {
case 'a':
temp = temp * 3;
break;
case 'b':
temp = temp * 5;
break;
case 'c':
temp = temp * 7;
break;
case 'd':
temp = temp * 11;
break;
case 'e':
temp = temp * 13;
break;
case 'f':
temp = temp * 17;
break;
case 'g':
temp = temp * 19;
break;
case 'h':
temp = temp * 23;
break;
case 'i':
temp = temp * 29;
break;
case 'j':
temp = temp * 31;
break;
case 'k':
temp = temp * 37;
break;
case 'l':
temp = temp * 41;
break;
case 'm':
temp = temp * 43;
break;
case 'n':
temp = temp * 47;
break;
case 'o':
temp = temp * 53;
break;
case 'p':
temp = temp * 59;
break;
case 'q':
temp = temp * 61;
break;
case 'r':
temp = temp * 67;
break;
case 's':
temp = temp * 71;
break;
case 't':
temp = temp * 73;
break;
case 'u':
temp = temp * 79;
break;
case 'v':
temp = temp * 83;
break;
case 'w':
temp = temp * 97;
break;
case 'x':
temp = temp * 101;
break;
case 'y':
temp = temp * 103;
break;
case 'z':
temp = temp * 107;
break;
}
}
return temp;
}
public static class MyComparator implements java.util.Comparator<String> {
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
}
*/