-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMapTwo.java
263 lines (222 loc) · 8.25 KB
/
MapTwo.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
/**
*
*/
package coding.bat.solutions;
import java.util.HashMap;
import java.util.Map;
/**
* @author Aman Shekhar
*
*/
public class MapTwo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
// Given an array of strings, return a Map<String, Integer> containing a key for
// every different string in the array, always with the value 0. For example the
// string "hello" makes the pair "hello":0. We'll do more complicated counting
// later, but for this problem the value is simply 0.
//
//
// word0(["a", "b", "a", "b"]) → {"a": 0, "b": 0}
// word0(["a", "b", "a", "c", "b"]) → {"a": 0, "b": 0, "c": 0}
// word0(["c", "b", "a"]) → {"a": 0, "b": 0, "c": 0}
public Map<String, Integer> word0(String[] strings) {
Map<String, Integer> map = new HashMap();
for (String s : strings) {
map.put(s, 0);
}
return map;
}
// --------------------------------------------------------------------------------------------
// Given an array of strings, return a Map<String, Integer> containing a key for
// every different string in the array, and the value is that string's length.
//
//
// wordLen(["a", "bb", "a", "bb"]) → {"bb": 2, "a": 1}
// wordLen(["this", "and", "that", "and"]) → {"that": 4, "and": 3, "this": 4}
// wordLen(["code", "code", "code", "bug"]) → {"code": 4, "bug": 3}
public Map<String, Integer> wordLen(String[] strings) {
Map<String, Integer> map = new HashMap();
for (String s : strings) {
map.put(s, s.length());
}
return map;
}
// --------------------------------------------------------------------------------------------
// Given an array of non-empty strings, create and return a Map<String, String>
// as follows: for each string add its first character as a key with its last
// character as the value.
//
//
// pairs(["code", "bug"]) → {"b": "g", "c": "e"}
// pairs(["man", "moon", "main"]) → {"m": "n"}
// pairs(["man", "moon", "good", "night"]) → {"g": "d", "m": "n", "n": "t"}
public Map<String, String> pairs(String[] strings) {
Map<String, String> map = new HashMap();
for (String s : strings) {
map.put(s.charAt(0) + "", s.charAt(s.length() - 1) + "");
}
return map;
}
// --------------------------------------------------------------------------------------------
// The classic word-count algorithm: given an array of strings, return a
// Map<String, Integer> with a key for each different string, with the value the
// number of times that string appears in the array.
//
//
// wordCount(["a", "b", "a", "c", "b"]) → {"a": 2, "b": 2, "c": 1}
// wordCount(["c", "b", "a"]) → {"a": 1, "b": 1, "c": 1}
// wordCount(["c", "c", "c", "c"]) → {"c": 4}
public Map<String, Integer> wordCount(String[] strings) {
Map<String, Integer> map = new HashMap();
for (String s : strings) {
String tmp = s;
if (map.containsKey(tmp)) {
int count = map.get(tmp);
map.put(tmp, count + 1);
} else
map.put(tmp, 1);
}
return map;
}
// --------------------------------------------------------------------------------------------
// Given an array of non-empty strings, return a Map<String, String> with a key
// for every different first character seen, with the value of all the strings
// starting with that character appended together in the order they appear in
// the array.
//
//
// firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t":
// "teatoast"}
// firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb",
// "c": "cccCC", "d": "d"}
// firstChar([]) → {}
//
public Map<String, String> firstChar(String[] strings) {
Map<String, String> map = new HashMap();
for (String s : strings) {
String key = s.charAt(0) + "";
if (map.containsKey(key)) {
String value = map.get(key) + s;
map.put(key, value);
} else
map.put(key, s);
}
return map;
}
// --------------------------------------------------------------------------------------------
//
//
// Loop over the given array of strings to build a result string like this: when
// a string appears the 2nd, 4th, 6th, etc. time in the array, append the string
// to the result. Return the empty string if no string appears a 2nd time.
//
//
// wordAppend(["a", "b", "a"]) → "a"
// wordAppend(["a", "b", "a", "c", "a", "d", "a"]) → "aa"
// wordAppend(["a", "", "a"]) → "a"
public String wordAppend(String[] strings) {
Map<String, Integer> map = new HashMap();
String string = "";
for (String s : strings) {
String key = s;
if (map.containsKey(key)) {
int value = map.get(key);
value++;
if (value % 2 == 0)
string += key;
map.put(key, value);
} else
map.put(key, 1);
}
return string;
}
// --------------------------------------------------------------------------------------------
// Given an array of strings, return a Map<String, Boolean> where each different
// string is a key and its value is true if that string appears 2 or more times
// in the array.
//
//
// wordMultiple(["a", "b", "a", "c", "b"]) → {"a": true, "b": true, "c": false}
// wordMultiple(["c", "b", "a"]) → {"a": false, "b": false, "c": false}
// wordMultiple(["c", "c", "c", "c"]) → {"c": true}
public Map<String, Boolean> wordMultiple(String[] strings) {
Map<String, Integer> stringCount = new HashMap();
Map<String, Boolean> map = new HashMap();
for (String s : strings) {
String key = s;
if (stringCount.containsKey(key)) {
int count = stringCount.get(key);
count++;
stringCount.put(key, count);
} else {
stringCount.put(key, 1);
}
map.put(key, stringCount.get(key) >= 2);
}
return map;
}
// --------------------------------------------------------------------------------------------
// We'll say that 2 strings "match" if they are non-empty and their first chars
// are the same. Loop over and then return the given array of non-empty strings
// as follows: if a string matches an earlier string in the array, swap the 2
// strings in the array. When a position in the array has been swapped, it no
// longer matches anything. Using a map, this can be solved making just one pass
// over the array. More difficult than it looks.
//
//
// allSwap(["ab", "ac"]) → ["ac", "ab"]
// allSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by",
// "cy", "cx", "bx", "ax", "azz", "aaa"]
// allSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by",
// "ax", "bx", "aj", "ai", "by", "bx"]
public String[] allSwap(String[] strings) {
Map<String, Integer> map = new HashMap();
for (int i = 0; i < strings.length; i++) {
if (map.containsKey(strings[i].charAt(0) + "")) {
String string = strings[i];
strings[i] = strings[map.get(strings[i].charAt(0) + "")];
strings[map.get(strings[i].charAt(0) + "")] = string;
map.remove(strings[i].charAt(0) + "");
} else
map.put(strings[i].charAt(0) + "", i);
}
return strings;
}
// --------------------------------------------------------------------------------------------
// We'll say that 2 strings "match" if they are non-empty and their first chars
// are the same. Loop over and then return the given array of non-empty strings
// as follows: if a string matches an earlier string in the array, swap the 2
// strings in the array. A particular first char can only cause 1 swap, so once
// a char has caused a swap, its later swaps are disabled. Using a map, this can
// be solved making just one pass over the array. More difficult than it looks.
//
//
// firstSwap(["ab", "ac"]) → ["ac", "ab"]
// firstSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by",
// "cy", "cx", "bx", "ax", "aaa", "azz"]
// firstSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by",
// "ax", "bx", "ai", "aj", "bx", "by"]
public String[] firstSwap(String[] strings) {
Map<String, Integer> map = new HashMap();
for (int i = 0; i < strings.length; i++) {
String string = String.valueOf(strings[i].substring(0, 1));
if (map.containsKey(string)) {
int value = map.get(string);
if (value == -1)
continue;
int pos = map.get(string);
String temp = strings[pos];
strings[pos] = strings[i];
strings[i] = temp;
map.put(string, -1);
} else
map.put(string, i);
}
return strings;
}
}