-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayString.java
413 lines (347 loc) · 11.9 KB
/
ArrayString.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import java.util.ArrayList;
import java.util.Objects;
public class ArrayString {
public static void main(String[] args) {
char[][] matrix = {
{'f', 'f', 'f', 'f', 'a'},
{'f', 'f', 'f', 'p', 'f'},
{'f', 'f', 'p', 'f', 'f'},
{'f', 'l', 'f', 'f', 'f'},
{'e', 'f', 'f', 'f', 'f'}};
ArrayList<String> words = new ArrayList<>();
words.add("apple");
ArrayList<String> matches = wordSearch(matrix, words);
matches.forEach(System.out::println);
}
public static ArrayList<String> wordSearch(char[][] matrix, ArrayList<String> words) {
ArrayList<String> result = new ArrayList<>();
ArrayList<String> diagonals = traverseDiagonals(matrix);
for(String word : words) {
for(String diagonal : diagonals) {
if(word.equals(diagonal) || diagonal.contains(word) )
result.add(word);
}
}
return result;
}
private static ArrayList<String> traverseDiagonals(char[][] matrix) {
ArrayList<String> diagonals = new ArrayList<>();
StringBuilder diagonalUp, diagonalDown;
int iteration = 0;
int maxCount = matrix.length + matrix[0].length - 1;
int topRow = 0, column = 0;
int bottomRow = matrix.length - 1;
while (iteration < maxCount) {
diagonalUp = new StringBuilder(getDiagonalUp(matrix, topRow, column));
diagonalDown = new StringBuilder(getDiagonalDown(matrix, bottomRow, column));
diagonals.add(diagonalUp.toString());
diagonals.add(diagonalUp.reverse().toString());
diagonals.add(diagonalDown.toString());
diagonals.add(diagonalDown.reverse().toString());
if (topRow < matrix.length - 1 || bottomRow > 0) {
topRow++;
bottomRow--;
} else if (column < matrix[0].length - 1) {
column++;
}
iteration++;
}
return diagonals;
}
private static String getDiagonalUp(char[][] matrix, int row, int column) {
StringBuilder sb = new StringBuilder();
while (row >= 0 && column < matrix[0].length) {
sb.append(matrix[row][column]);
row--;
column++;
}
return sb.toString();
}
private static String getDiagonalDown(char[][] matrix, int row, int column) {
StringBuilder sb = new StringBuilder();
while (row < matrix.length && column < matrix[0].length) {
sb.append(matrix[row][column]);
row++;
column++;
}
return sb.toString();
}
public static int balencedPar(String str) {
final Character L_PAR = '(';
final Character R_PAR = ')';
Character next;
int count = 0;
java.util.Stack<Character> store = new java.util.Stack<>();
for (int i = 0; i < str.length(); ++i) {
next = str.charAt(i);
if (next == L_PAR) store.push(next);
else if ((next == R_PAR) && (!store.empty())) {
store.pop();
count++;
} else if ((next == R_PAR) && (store.empty())) {
return -1;
}
}
return count;
}
//1.1
//check using hashset
public static boolean uniqueString(String s) {
java.util.HashSet<Character> hash = new java.util.HashSet<>();
for (Character c : s.toCharArray()) {
if (!hash.add(c)) return false;
}
return true;
}
//1.1
//fill array length of all ASCII chars with false
//check each char in the string convert to ascii int val
//check if val in boolArray is true (already match)
public static boolean uniqueStringUsingArray(String str) {
boolean[] boolArray = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (boolArray[val]) {
return false;
}
boolArray[val] = true;
}
return true;
}
//1.2
//stringbuilder used since faster than stringbuffer (via async) and no need for threadsafe
public static String reverseString(String str) {
StringBuilder reverse = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reverse.append(str.charAt(i));
}
return reverse.toString();
}
//1.3 solution #1
//Anagrams have same characters in different order
//Check if same after sorting
private static String sortString(String str) {
char[] charArray = str.toCharArray();
java.util.Arrays.sort(charArray);
return new String(charArray);
}
public static boolean permCheck(String s1, String s2) {
return s1.length() == s2.length() && sortString(s1).equals(sortString(s2));
}
//1.3 solution #2
//Count how many times each char appears
//compare the two arrays
public static boolean permCheck2(String s1, String s2) {
if (s1.length() != s2.length()) return false;
int letters[] = new int[256];
char charArray[] = s1.toCharArray();
for (char c : charArray) {
letters[c]++;
}
for (int i = 0; i < s2.length(); i++) {
int charVal = (int) s2.charAt(i);
if (--letters[charVal] < 0) {
return false;
}
}
return true;
}
//1.5
//copy each character to a new string and count the repeats
//O(N) size and O(N) space
public static String compressString(String str) {
int size = countCompression(str);
if (size >= str.length()) {
return str;
}
StringBuilder compressed = new StringBuilder();
char temp = str.charAt(0);
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == temp) {
count++;
} else {
compressed.append(temp);
compressed.append(count);
temp = str.charAt(i);
count = 1;
}
}
compressed.append(temp);
compressed.append(count);
return compressed.toString();
}
//check if compressed is smaller than orig
public static int countCompression(String str) {
if (str == null || str.isEmpty()) return 0;
char last = str.charAt(0);
int size = 0;
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == last) count++;
else {
last = str.charAt(i);
size += 1 + String.valueOf(count).length(); //digit count
count = 1;
}
}
size += 1 + String.valueOf(count).length();
return size;
}
public static int[][] rotateMatrix(int[][] matrix, int flag) {
int m = matrix.length;
int n = matrix[0].length;
int[][] ret = new int[n][m];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (flag == 0) ret[m - 1 - c][r] = matrix[r][c];
else if (flag == 1) ret[c][m - 1 - r] = matrix[r][c];
else throw new Error("Invalid");
}
}
return ret;
}
//1.7
public static void setToZero(int[][] matrix) {
int M = matrix.length;
int N = matrix[0].length;
boolean row[] = new boolean[M];
boolean col[] = new boolean[N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
//1.8
public static boolean isRotation(String s1, String s2) {
if (s1.length() != s2.length()) return false;
int length = s1.length();
int offset = 0;
for (int i = 0; i < length; i++) {
if (s1.charAt(0) == s2.charAt(i)) {
offset = i;
break;
}
}
for (int i = 0; i < length; i++) {
if (s1.charAt(i) == s2.charAt(offset)) {
offset++;
offset %= length; //don't go past size limit
} else return false;
}
return true;
}
public static boolean isRotationBook(String s1, String s2) {
if (s1.length() != s2.length()) return false;
String s1Double = s1 + s1;
return s1Double.contains(s2);
}
public static void findFirst(String str) {
java.util.HashMap<Character, Integer> map = new java.util.HashMap<>();
for (char c : str.toCharArray()) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
for (java.util.Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
System.out.println(entry.getKey());
break;
}
}
}
public static void oddOccurence(int[] arr) {
java.util.HashMap<Integer, Integer> map = new java.util.HashMap<>();
for (int i : arr) {
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
for (java.util.Map.Entry<Integer, Integer> item : map.entrySet()) {
if (item.getValue() % 2 != 0) System.out.print(item.getKey());
}
}
//17.1
//swap values in place
public static void inplaceSwap(int a, int b) {
a = a - b;
b = a + b;
a = b - a;
}
//17.2
//NxN tic tac toe winner check
public char TTTWon(char[][] board) {
int N = board.length;
int row;
int col;
//check rows
for (row = 0; row < N; row++) {
if (board[row][0] != ' ') {
for (col = 1; col < N; col++) {
if (board[row][col] != board[row][col - 1]) break;
}
if (col == N) return board[row][0];
}
}
//check cols
for (col = 0; col < N; col++) {
if (board[0][col] != ' ') {
for (row = 1; row < N; row++) {
if (board[row][col] != board[row - 1][col]) break;
}
if (row == N) return board[0][col];
}
}
//check diagonal top left to bottom right
if (board[0][0] != ' ') {
for (row = 1; row < N; row++) {
if (board[row][row] != board[row - 1][row - 1]) break;
}
if (row == N) return board[0][0];
}
//check diagonal top left to bottom right
if (board[N - 1][0] != ' ') {
for (row = 1; row < N; row++) {
if (board[N - row - 1][row] != board[N - row][row - 1]) break;
}
if (row == N) return board[N - 1][0];
}
return ' ';
}
//17.6
//get max sum of values in an array
public static int getMaxSum(int[] arr) {
int maxSum = 0;
int sum = 0;
for (int i : arr) {
sum += i;
if (maxSum < sum) maxSum = sum;
else if (sum < 0) sum = 0;
}
return maxSum;
}
public static String reverseWords(String sentence) {
String[] parts = sentence.split(" ");
StringBuilder builder = new StringBuilder();
builder.append(parts[parts.length - 1]);
for (int i = parts.length - 2; i >= 0; --i) {
builder.append(" ").append(parts[i]);
}
return builder.toString();
}
}