Skip to content

Commit 1d6ea95

Browse files
committed
Added 2 solutions & modified 1 solution
1 parent 607de4a commit 1d6ea95

File tree

3 files changed

+90
-58
lines changed

3 files changed

+90
-58
lines changed

Easy/Running Sum of 1d Array.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int[] runningSum(int[] nums) {
3+
int[] ans = new int[nums.length];
4+
int sum = 0;
5+
for (int i = 0; i < nums.length; i++) {
6+
sum += nums[i];
7+
ans[i] = sum;
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int findLeastNumOfUniqueInts(int[] arr, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
List<Integer> list = new ArrayList<>();
5+
for (int num : arr) {
6+
if (!map.containsKey(num)) {
7+
list.add(num);
8+
}
9+
map.put(num, map.getOrDefault(num, 0) + 1);
10+
}
11+
Collections.sort(list, new Comparator<Integer>(){
12+
public int compare(Integer o1, Integer o2) {
13+
return map.get(o1) - map.get(o2);
14+
}
15+
});
16+
for (Integer num : list) {
17+
int count = map.get(num);
18+
k -= count;
19+
if (k >= 0) {
20+
map.remove(num);
21+
}
22+
else {
23+
break;
24+
}
25+
}
26+
return map.size();
27+
}
28+
}

Medium/Replace Words.java

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
11
class Solution {
2-
3-
public String replaceWords(List<String> dict, String sentence) {
4-
5-
Map<String, Integer> map = new HashMap<>();
6-
7-
for (String s: dict) {
8-
map.put(s, s.length());
9-
}
10-
11-
Map<String, Integer> sortedMap = sortByValue(map);
12-
13-
String[] arr = sentence.split("\\s");
14-
String[] ans = new String[arr.length];
15-
int j = 0;
16-
17-
for (String s : arr) {
18-
boolean gotIt = false;
19-
for (Map.Entry<String,Integer> entry : sortedMap.entrySet()) {
20-
if (s.startsWith(entry.getKey())) {
21-
ans[j] = entry.getKey();
22-
gotIt = true;
23-
break;
24-
}
25-
}
26-
27-
if (!gotIt) {
28-
ans[j] = s;
29-
}
30-
31-
j++;
32-
}
33-
34-
StringBuilder ansString = new StringBuilder("");
35-
36-
for (String s : ans) {
37-
ansString.append(s + " ");
38-
}
39-
40-
return ansString.toString().trim();
2+
public String replaceWords(List<String> dict, String sentence) {
3+
Node root = new Node('-');
4+
for (String word : dict) {
5+
addWord(word, 0, root);
416
}
42-
43-
private Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) {
44-
45-
List<Map.Entry<String, Integer>> list =
46-
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
47-
48-
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
49-
public int compare(Map.Entry<String, Integer> o1,
50-
Map.Entry<String, Integer> o2) {
51-
return (o1.getValue()).compareTo(o2.getValue());
52-
}
53-
});
54-
55-
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
56-
for (Map.Entry<String, Integer> entry : list) {
57-
sortedMap.put(entry.getKey(), entry.getValue());
58-
}
59-
60-
return sortedMap;
7+
String[] words = sentence.split("\\s+");
8+
for (int i = 0; i < words.length; i++) {
9+
String prefix = getPrefix(words[i], root);
10+
if (prefix != null) {
11+
words[i] = prefix;
12+
}
13+
}
14+
return String.join(" ", words);
15+
}
16+
17+
private String getPrefix(String word, Node root) {
18+
for (int i = 0; i < word.length(); i++) {
19+
if (!root.children.containsKey(word.charAt(i))) {
20+
return null;
21+
}
22+
root = root.children.get(word.charAt(i));
23+
if (root.word != null) {
24+
return root.word;
25+
}
6126
}
27+
return null;
28+
}
29+
30+
private void addWord(String word, int idx, Node root) {
31+
if (idx == word.length()) {
32+
return;
33+
}
34+
if (!root.children.containsKey(word.charAt(idx))) {
35+
root.children.put(word.charAt(idx), new Node(word.charAt(idx)));
36+
}
37+
root = root.children.get(word.charAt(idx));
38+
if (idx == word.length() - 1) {
39+
root.word = word;
40+
}
41+
addWord(word, idx + 1, root);
42+
}
43+
}
44+
6245

46+
class Node {
47+
char c;
48+
Map<Character, Node> children;
49+
String word;
50+
51+
public Node(char c) {
52+
this.c = c;
53+
children = new HashMap<>();
54+
word = null;
55+
}
6356
}

0 commit comments

Comments
 (0)