-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPrbs.java
More file actions
241 lines (195 loc) · 5.9 KB
/
Copy pathStringPrbs.java
File metadata and controls
241 lines (195 loc) · 5.9 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
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
class Pair {
int freq;
char ele;
Pair(int freq, char ele) {
this.freq = freq;
this.ele = ele;
}
}
class StringSolutions {
// Longest Common Prefix
public String longestCommonPrefix(String[] str) {
// Brute
/**
* String sample = str[0], temp;
* String prefix = "";
* for (int wordIdx = 1; wordIdx < str.length; wordIdx++) {
* temp = "";
* for (int charIdx = 0; charIdx < Math.min(sample.length(),
* str[wordIdx].length()); charIdx++) {
* if (str[wordIdx].charAt(charIdx) == sample.charAt(charIdx)) {
* temp += sample.charAt(charIdx);
* } else {
* break;
* }
* }
* if (temp == "") {
* prefix = "";
* break;
* }
* if (prefix.length() == 0 || temp.length() < prefix.length()) {
* prefix = temp;
* }
* }
*
* return prefix;
*/
// Optimal O(M * N log N) time & O(M) space
Arrays.sort(str);
String first = str[0];
String last = str[str.length - 1];
StringBuilder prefix = new StringBuilder();
for (int idx = 0; idx < Math.min(first.length(), last.length()); idx++) {
if (first.charAt(idx) != last.charAt(idx)) {
break;
} else {
prefix.append(first.charAt(idx));
}
}
return prefix.toString();
}
// Reverse String
public void reverseString(List<Character> s) {
int left = 0;
int right = s.size() - 1;
char temp;
while (left < right) {
temp = s.get(right);
s.set(right, s.get(left));
s.set(left, temp);
left++;
right--;
}
for (char c : s) {
System.out.println(c);
}
}
// Palindrome Check string
public boolean palindromeCheck(String s) {
// int lastIdx = s.length() - 1;
// StringBuilder reverse = new StringBuilder();
// while (lastIdx >= 0) {
// reverse.append(s.charAt(lastIdx));
// lastIdx--;
// }
// return s.equals(reverse.toString());
// Approach 2
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
// Largest Odd Number in a String
public String largestOddNumberString(String s) {
// Optimal O(n) time & O(n) space
int n = s.length();
int endIdx = -1;
for (int idx = n - 1; idx >= 0; idx--) {
if ((s.charAt(idx) - '0') % 2 == 1) {
endIdx = idx;
break;
}
}
if (endIdx == -1) {
return "";
}
int startIdx = 0;
for (startIdx = 0; startIdx < n; startIdx++) {
if (s.charAt(startIdx) != '0') {
break;
}
}
return s.substring(startIdx, endIdx + 1);
}
// Isomorphic Strings
public boolean isomorphicString(String s, String t) {
// Optimal O(N) time & O(2 * 256) space
int[] mapS = new int[256], mapT = new int[256];
for (int idx = 0; idx < s.length(); idx++) {
if (mapS[s.charAt(idx)] != mapT[t.charAt(idx)])
return false;
mapS[s.charAt(idx)] = idx + 1;
mapT[t.charAt(idx)] = idx + 1;
}
return true;
}
// Rotate String
public boolean rotateString(String s, String goal) {
// Brute
if (s.length() != goal.length()) {
return false;
}
if (s.length() == 1) {
return s.equals(goal);
}
int shifts = s.length();
while (shifts > 0) {
s = s.substring(1) + s.charAt(0);
if (s.equals(goal)) {
return true;
}
shifts--;
}
return false;
// Optimimal
// return s.length() == goal.length() && (s + s).contains(goal);
}
// Valid Anagram
public boolean isAnagram(String s, String t) {
// Optimal O(N) time & 2 * O(26) space
if (s.length() != t.length()) {
return false;
}
int[] freqS = new int[26], freqT = new int[26];
for (int idx = 0; idx < s.length(); idx++) {
freqS[s.charAt(idx) - 'a']++;
freqT[t.charAt(idx) - 'a']++;
}
for (int charIdx = 0; charIdx < freqS.length; charIdx++) {
if (freqS[charIdx] != freqT[charIdx]) {
return false;
}
}
return true;
}
// Sort Characters By Frequency
public List<Character> sortByFrequency(String s) {
// Optimal O(26) + O(n) + O(26 log 26) + O(26) time & O(26) + O(26)
// => O(n) time and O(1) space
Pair[] hash = new Pair[26];
for (int idx = 0; idx < hash.length; idx++) {
hash[idx] = new Pair(0, (char) (idx + 'a'));
}
for (int idx = 0; idx < s.length(); idx++) {
hash[s.charAt(idx) - 'a'].freq++;
}
Arrays.sort(hash, (p1, p2) -> {
if (p1.freq != p2.freq)
return p2.freq - p1.freq;
return p1.ele - p2.ele;
});
List<Character> result = new ArrayList<>();
for (int idx = 0; idx < hash.length; idx++) {
if (hash[idx].freq == 0) {
break;
}
result.add(hash[idx].ele);
}
return result;
}
}
public class StringPrbs {
public static void main(String[] args) {
StringSolutions solution = new StringSolutions();
System.out.println(solution.isomorphicString("paper", "title"));
}
}