Skip to content

Commit c42e396

Browse files
add 1258
1 parent ba869c3 commit c42e396

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ _If you like this project, please leave me a star._ ★
4646
|1265|[Print Immutable Linked List in Reverse](https://leetcode.com/problems/print-immutable-linked-list-in-reverse//)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1265.java) | |Medium||
4747
|1261|[Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1261.java) ||Medium|Tree, HashTable|
4848
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
49+
|1258|[Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1258.java) ||Medium|Backtracking|
4950
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | |Easy||
5051
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | |Easy||
5152
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
/**
13+
* 1258. Synonymous Sentences
14+
*
15+
* Given a list of pairs of equivalent words synonyms and a sentence text, Return all possible synonymous sentences sorted lexicographically.
16+
*
17+
* Example 1:
18+
* Input:
19+
* synonyms = [["happy","joy"],["sad","sorrow"],["joy","cheerful"]],
20+
* text = "I am happy today but was sad yesterday"
21+
* Output:
22+
* ["I am cheerful today but was sad yesterday",
23+
* ​​​​​​​"I am cheerful today but was sorrow yesterday",
24+
* "I am happy today but was sad yesterday",
25+
* "I am happy today but was sorrow yesterday",
26+
* "I am joy today but was sad yesterday",
27+
* "I am joy today but was sorrow yesterday"]
28+
*
29+
* Constraints:
30+
* 0 <= synonyms.length <= 10
31+
* synonyms[i].length == 2
32+
* synonyms[0] != synonyms[1]
33+
* All words consist of at most 10 English letters only.
34+
* text is a single space separated sentence of at most 10 words.
35+
* */
36+
public class _1258 {
37+
public static class Solution1 {
38+
public List<String> generateSentences(List<List<String>> synonyms, String text) {
39+
String[] words = text.split(" ");
40+
Map<String, Set<String>> map = buildSynonymDict(words, synonyms);
41+
Set<String> result = new HashSet<>();
42+
result.add(text);
43+
for (int i = 0; i < words.length; i++) {
44+
List<String> list = new ArrayList<>();
45+
for (String next : result) {
46+
list.add(next);
47+
list.addAll(findAllSynonymForThisWord(next, i, map));
48+
}
49+
result.addAll(list);
50+
}
51+
List<String> list = new ArrayList<>();
52+
list.addAll(result);
53+
Collections.sort(list);
54+
return list;
55+
}
56+
57+
private List<String> findAllSynonymForThisWord(String sentence, int i, Map<String, Set<String>> map) {
58+
String[] words = sentence.split(" ");
59+
List<String> list = new ArrayList<>();
60+
Set<String> synonyms = map.get(words[i]);
61+
for (String s : synonyms) {
62+
words[i] = s;
63+
list.add(formWord(words));
64+
}
65+
return list;
66+
}
67+
68+
private String formWord(String[] words) {
69+
StringBuilder sb = new StringBuilder();
70+
for (String word : words) {
71+
sb.append(word);
72+
sb.append(" ");
73+
}
74+
return sb.substring(0, sb.length() - 1);
75+
}
76+
77+
private Map<String, Set<String>> buildSynonymDict(String[] words, List<List<String>> synonyms) {
78+
Map<String, Set<String>> map = new HashMap<>();
79+
for (String key : words) {
80+
if (!map.containsKey(key)) {
81+
map.put(key, new HashSet<>());
82+
}
83+
map.get(key).add(key);
84+
}
85+
for (List<String> list : synonyms) {
86+
for (String key : map.keySet()) {
87+
if (map.get(key).contains(list.get(0))) {
88+
map.get(key).add(list.get(1));
89+
} else if (map.get(key).contains(list.get(1))) {
90+
map.get(key).add(list.get(0));
91+
}
92+
}
93+
}
94+
return map;
95+
}
96+
97+
}
98+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1258;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1258Test {
14+
15+
private static _1258.Solution1 solution1;
16+
private static List<List<String>> synonyms;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1258.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
synonyms = Arrays.asList(Arrays.asList("happy", "joy"), Arrays.asList("sad", "sorrow"), Arrays.asList("joy", "cheerful"));
26+
List<String> expected = Arrays.asList("I am cheerful today but was sad yesterday", "I am cheerful today but was sorrow yesterday", "I am happy today but was sad yesterday", "I am happy today but was sorrow yesterday",
27+
"I am joy today but was sad yesterday", "I am joy today but was sorrow yesterday");
28+
assertEquals(expected, solution1.generateSentences(synonyms, "I am happy today but was sad yesterday"));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)