-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.java
More file actions
301 lines (219 loc) · 8.4 KB
/
Copy pathRunner.java
File metadata and controls
301 lines (219 loc) · 8.4 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
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
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Andrea
* @implNote Hacker Rank implementations
*
*/
public class Runner {
/**
*
*/
public static String getSmallestAndLargest(String s, int k) {
// Complete the function
// 'smallest' must be the lexicographically smallest substring of length 'k'
// 'largest' must be the lexicographically largest substring of length 'k'
java.util.SortedSet<String> array = new java.util.TreeSet<String>();
for (int index = 0; index <= (s.length() - k); index ++ ) {
array.add(s.substring(index, index + k));
}
String smallest = array.first();
String largest = array.last();
return smallest + "\n" + largest;
}
/*
* Check if a string is Palindrome
*/
public static String isPalindrome(String S) {
char array[] = S.toCharArray();
int arrayLength = S.length() - 1;
for (int index = 0; index < S.length(); index++ ) {
if ( array[index] != array[arrayLength - index] ) {
return "No";
}
}
return "Yes";
}
/*
* Check if a string is Anagram
*/
public static String isAnagram(String a, String b) {
if ( ( a.length() < 1 && a.length() > 50 ) || ( b.length() < 1 && b.length() > 50 ) || ( a.length() != b.length() )){
return "Not Anagrams";
}
char[] s1Array = a.toLowerCase().toCharArray();
char[] s2Array = b.toLowerCase().toCharArray();
//Sorting both s1Array and s2Array
java.util.Arrays.sort(s1Array);
java.util.Arrays.sort(s2Array);
//Checking whether s1Array and s2Array are equal
if ( java.util.Arrays.equals(s1Array, s2Array) ) {
return "Anagrams";
} else {
return "Not Anagrams";
}
}
/*
* Tokenize
*/
public void tokenize(String s) {
if ( s.trim().equals("") ) {
System.out.println("0");
return;
} else if(s.length() > 400000){
return;
}
String[] tokens = s.trim().split("[_@ !,?.']+");
System.out.println(tokens.length);
for ( int index = 0; index < tokens.length; index++ ) {
System.out.println(tokens[index]);
}
}
public void isRegEx(String pattern) {
try {
if ( java.util.regex.Pattern.compile(pattern) != null ) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
} catch (Exception ex) {
System.out.println("Invalid");
}
}
/* Sock merchant */
public int sockMerchant(int n, int[] ar) {
boolean stop = false;
int pairs = 0;
do {
int leftSock = 0;
for ( int index = 0; index < ar.length; index++ ) {
// Find sock if left sock exists
if ( leftSock > 0 && leftSock == ar[index] ) {
pairs++;
ar[index] = 0;
break;
} else if ( leftSock > 0 ) {
// Serach sock mode
continue;
}
// Halt control
if ( leftSock > 0 && index == ( ar.length -1) ) {
break;
}
// Halt control
if ( leftSock == 0 && index == ( ar.length -1) ) {
stop = true;
break;
}
// Pick a left sock
if ( ar[index] != 0 ) {
leftSock = ar[index];
ar[index] = 0;
continue;
}
}
} while (!stop);
return pairs;
}
/* Fibonacci */
public int fibonacci(int n) {
if (n < 2) {
return n;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
/**
* Work in progress
* @param s
* @return
*/
static String isValid(String s) {
java.util.Map<Character, Integer> map = new java.util.HashMap<Character, Integer>();
// in case of a aa aaa or aab or abc it works
if (s.length() <= 3) {
return "YES";
}
char[] chars = s.toCharArray();
java.util.Arrays.sort(chars);
for ( int index = 0; index < s.length(); index++ ) {
if ( !map.containsKey(chars[index]) ) {
map.put(chars[index], 1);
} else {
map.put(chars[index], map.get(chars[index]) + 1);
}
}
return map.toString();
}
public static int evaluateBinary(BigInteger binary, int counter) {
if ( binary.equals(BigInteger.ZERO) ) {
return counter;
}
if ( binary.mod(BigInteger.TWO).equals(BigInteger.ZERO) ) {
return evaluateBinary(binary.divide(BigInteger.TWO), ++counter);
} else {
return evaluateBinary(binary.subtract(BigInteger.ONE), ++counter);
}
}
/**
* Given string S of length N which encodes a non-negative number V in a binary for.
* If V is odd, substract 1
* If V is even, divide it by 2
* The formula is: <number of ones> + <number of digits> - <index of left most one> - 1
*
* The idea is that the division operation really is a digit-shift to the right, with the rightmost zero dropping off. It reduces the number of digits by 1.
* It is also the only way to reduce the number of digits. So you need as many divisions as there are digits in your input, with two exceptions:
* We should not count pre-padded zeroes.
* When only one significant digit is left over (a 1-digit), no division will be needed: just a subtraction will be enough.
* So this means we will need to discount prepadded zeroes (or find the position of the first 1-digit) and count the number of digits from there on,
* and subtract one to compensate for the final operation which will not involve a division.
*
* On top of that we need one subtraction operation for any 1-digit in the input: all 1-digits will sooner or later get shifted into the right most position
* at which moment a subtraction is necessary to get rid of it.
*
* @param s
* @return
*/
public static int solution(String s) {
int firstOneAt = s.indexOf("1");
return firstOneAt == -1 ? 0 : s.replace("0", "").length() + s.length() - firstOneAt - 1;
}
/**
* Particle Velocity quiz
* @param A
* @return
*/
public static int stableVelocity(int[] A) {
if ( A.length < 3 ) {
return 0;
}
int totalPeriods = 0;
int delta = A[1] - A[0];
int sameDelta = 0;
for ( int index = 2; index < A.length; index++ ) {
if ( A[index] - A[index - 1] == delta ) {
totalPeriods += 1 + sameDelta;
sameDelta++;
} else {
sameDelta = 0;
delta = A[index] - A[index - 1];
}
}
return totalPeriods < 1000000000 ? totalPeriods : -1;
}
public static void main(String[] args) {
// System.out.println(getSmallestAndLargest("welcometojava", 3));
// System.out.println(isPalindrome("AABBCBBAAA"));
// System.out.println(isAnagram("CAT", "TAC"));
// tokenize(" ");
// isRegEx("[AZ[a-z](a-z)");
// System.out.println(sockMerchant(20, new int[] {4, 5, 5, 5, 6, 6, 4, 1, 4, 4, 3, 6, 6, 3, 6, 1, 4, 5, 5, 5}));
// System.out.println(fibonacci(100));
// System.out.println(isValid("aabbccddeefghi"));
System.out.println(stableVelocity(new int[] {4, 5, 5, 5, 6, 6, 4, 1, 4, 4, 3, 6, 6, 3, 6, 1, 4, 5, 5, 5}));
System.out.println(solution("1111010101111"));
}
}