-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter1.java
More file actions
194 lines (178 loc) · 4.86 KB
/
Copy pathchapter1.java
File metadata and controls
194 lines (178 loc) · 4.86 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
import java.util.*;
public class chapter1 {
public static class Tuple{
public int x;
public int y;
public Tuple(int x, int y){
this.x=x;
this.y=y;
}
}
public static class CompareTuple implements Comparator<Tuple>{
public int compare(Tuple t1, Tuple t2){
return t1.x-t2.x;
}
}
//Calculates the number of paths from the top left corner to bottom right corner of a nxm matrix,
//Paths can only cross entries that are true in isValid
public int numPaths(boolean[][] isValid, int n, int m){
int[][] count = new int[n][m];
for (int i=0;i<n;i++){
if(isValid[i][0]){
count[i][0]=1;
}
}
for (int i=0;i<m;i++){
if(isValid[0][i]){
count[0][i]=1;
}
}
for (int i=1;i<n;i++){
for (int j=1;j<m;j++){
if(isValid[i][j]){
count[i][j] = count[i-1][j]+count[j-1][i];
}
}
}
return count[n-1][m-1];
}
public static boolean uniqueChars(String s){
for (int i=0; i<s.length(); i++){
char currentChar = s.charAt(i);
if (i == s.length()-1) return true;
for (int j=i+1; j<s.length();j++){
if (currentChar == s.charAt(j)){
return false;
}
}
}
return true;
}
//Reverses a C-style string (last character is null)
public static String stringReverse(String s){
char[] data = s.toCharArray();
String result = "";
for (int i=1; i<data.length; i++){
result = result + data[data.length-i-1];
}
result = result+null;
return result;
}
public static String removeDuplicates(String input){
String result = "";
for (int i=0; i<input.length(); i++){
char currentChar = input.charAt(i);
boolean seen = false;
for (int j=0; j<i; j++){
seen = seen || (currentChar == input.charAt(j));
}
if (!seen){
result = result + currentChar;
}
}
return result;
}
public static HashMap<Character,Integer> countChars(char[] charList){
HashMap<Character, Integer> h1 = new HashMap<Character, Integer>();
for (int i=0; i<charList.length; i++){
if (!h1.containsKey(charList[i])){
h1.put(charList[i], 1);
}
else {
h1.put(charList[i],h1.get(charList[i]) + 1);
}
}
return h1;
}
public static boolean anagrams(String s1, String s2){
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();
HashMap<Character, Integer> h1 = countChars(chars1);
HashMap<Character, Integer> h2 = countChars(chars2);
return h1.equals(h2);
}
public static int intersectsMost(int[][] intervals){
List<Tuple> endpoints = new ArrayList<Tuple>();
for (int i=0; i<intervals.length; i++){
Tuple beginPoint = new Tuple(intervals[i][0],1);
Tuple endPoint = new Tuple(intervals[i][1],-1);
endpoints.add(beginPoint);
endpoints.add(endPoint);
}
Collections.sort(endpoints, new CompareTuple());
int intersects = 0;
int maxIntersects = 0;
for (int i=0; i<endpoints.size(); i++){
intersects = intersects + endpoints.get(i).y;
if (intersects>maxIntersects){
maxIntersects = intersects;
}
}
return maxIntersects;
}
public static String stringReplace(String input){
String result = "";
for (int i=0; i<input.length(); i++){
if (input.charAt(i) == ' '){
result += "%20";
}
else result += input.charAt(i);
}
return result;
}
//Performs a 90 degree clockwise rotation on a nxn image matrix
public static int[][] rotate90(int[][] image){
int[][] result = new int[image.length][image.length];
for (int i=0; i<image.length; i++){
for (int j=0; j<image.length; j++){
result[i][j] = image[j][image.length-i];
}
}
return result;
}
//Replace rows/columns with a 0 with rows/columns of all 0s
public static int[][] makeZeros(int[][] input){
if (input==null) return null;
int[] rowZeros = new int[input[0].length];
int[] columnZeros = new int[input.length];
for (int i=0; i<input[0].length; i++){
for (int j=0; j<input.length; j++){
if (input[i][j] == 0){
rowZeros[i] = 1;
columnZeros[j] = 1;
}
}
}
for (int i=0; i<input[0].length; i++){
for (int j=0; j<input.length; j++){
if (rowZeros[i] == 1 || columnZeros[j] == 1) {
input[i][j] = 0;
}
}
}
return input;
}
//Returns true if s2 is a rotation of s1
public static boolean isRotation(String s1, String s2){
if (s1.length() != s2.length()) return false;
String concatenated = s1 + s1;
if (concatenated.contains(s2)) return true;
return false;
}
public static void main(String[] args){
String test1 = "abcdea";
String test2 = "abcdef";
String test3 = "";
String test4 = "alopo";
String test5 = "waterbottle";
String test6 = "adebca";
String test7 = "hello world I'm here";
System.out.println(removeDuplicates(test1));
System.out.println(removeDuplicates(test2));
System.out.println(removeDuplicates(test3));
System.out.println(removeDuplicates(test4));
System.out.println(uniqueChars(test5));
System.out.println(anagrams(test1,test6));
System.out.println(isRotation(test5,"rbottlewate"));
}
}