Skip to content

Commit 826d27a

Browse files
committed
feat: add solutions to lc problem: No.1181. Before and After Puzzle
1 parent f815207 commit 826d27a

File tree

4 files changed

+152
-6
lines changed

4 files changed

+152
-6
lines changed

Diff for: solution/1100-1199/1181.Before and After Puzzle/README.md

+52-3
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,76 @@
5959
<li><code>1 &lt;= phrases[i].length &lt;= 100</code></li>
6060
</ul>
6161

62-
6362
## 解法
6463

6564
<!-- 这里可写通用的实现逻辑 -->
6665

66+
“哈希表 + 排序”实现。
67+
6768
<!-- tabs:start -->
6869

6970
### **Python3**
7071

7172
<!-- 这里可写当前语言的特殊实现逻辑 -->
7273

7374
```python
74-
75+
class Solution:
76+
def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]:
77+
same_first_word = collections.defaultdict(set)
78+
for i, phrase in enumerate(phrases):
79+
same_first_word[phrase.split()[0]].add(i)
80+
res = set()
81+
for i, phrase in enumerate(phrases):
82+
words = phrase.split()
83+
last_word = words[-1]
84+
if last_word in same_first_word:
85+
for j in same_first_word[last_word]:
86+
if i != j:
87+
res.add(' '.join(words[:-1] + phrases[j].split()))
88+
return sorted(list(res))
7589
```
7690

7791
### **Java**
7892

7993
<!-- 这里可写当前语言的特殊实现逻辑 -->
8094

8195
```java
82-
96+
class Solution {
97+
public List<String> beforeAndAfterPuzzles(String[] phrases) {
98+
Map<String, Set<Integer>> sameFirstWord = new HashMap<>();
99+
for (int i = 0; i < phrases.length; ++i) {
100+
String phrase = phrases[i];
101+
String word = phrase.split(" ")[0];
102+
if (!sameFirstWord.containsKey(word)) {
103+
sameFirstWord.put(word, new HashSet<>());
104+
}
105+
sameFirstWord.get(word).add(i);
106+
}
107+
Set<String> res = new HashSet<>();
108+
for (int i = 0; i < phrases.length; ++i) {
109+
String phrase = phrases[i];
110+
String[] words = phrase.split(" ");
111+
String lastWord = words[words.length - 1];
112+
if (sameFirstWord.containsKey(lastWord)) {
113+
for (int j : sameFirstWord.get(lastWord)) {
114+
if (i != j) {
115+
List<String> t = new ArrayList<>();
116+
for (int k = 0; k < words.length - 1; ++k) {
117+
t.add(words[k]);
118+
}
119+
for (String word : phrases[j].split(" ")) {
120+
t.add(word);
121+
}
122+
res.add(String.join(" ", t));
123+
}
124+
}
125+
}
126+
}
127+
List<String> output = new ArrayList<>(res);
128+
Collections.sort(output);
129+
return output;
130+
}
131+
}
83132
```
84133

85134
### **...**

Diff for: solution/1100-1199/1181.Before and After Puzzle/README_EN.md

+50-3
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,68 @@
5656
<li><code>1 &lt;= phrases[i].length &lt;= 100</code></li>
5757
</ul>
5858

59-
6059
## Solutions
6160

6261
<!-- tabs:start -->
6362

6463
### **Python3**
6564

6665
```python
67-
66+
class Solution:
67+
def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]:
68+
same_first_word = collections.defaultdict(set)
69+
for i, phrase in enumerate(phrases):
70+
same_first_word[phrase.split()[0]].add(i)
71+
res = set()
72+
for i, phrase in enumerate(phrases):
73+
words = phrase.split()
74+
last_word = words[-1]
75+
if last_word in same_first_word:
76+
for j in same_first_word[last_word]:
77+
if i != j:
78+
res.add(' '.join(words[:-1] + phrases[j].split()))
79+
return sorted(list(res))
6880
```
6981

7082
### **Java**
7183

7284
```java
73-
85+
class Solution {
86+
public List<String> beforeAndAfterPuzzles(String[] phrases) {
87+
Map<String, Set<Integer>> sameFirstWord = new HashMap<>();
88+
for (int i = 0; i < phrases.length; ++i) {
89+
String phrase = phrases[i];
90+
String word = phrase.split(" ")[0];
91+
if (!sameFirstWord.containsKey(word)) {
92+
sameFirstWord.put(word, new HashSet<>());
93+
}
94+
sameFirstWord.get(word).add(i);
95+
}
96+
Set<String> res = new HashSet<>();
97+
for (int i = 0; i < phrases.length; ++i) {
98+
String phrase = phrases[i];
99+
String[] words = phrase.split(" ");
100+
String lastWord = words[words.length - 1];
101+
if (sameFirstWord.containsKey(lastWord)) {
102+
for (int j : sameFirstWord.get(lastWord)) {
103+
if (i != j) {
104+
List<String> t = new ArrayList<>();
105+
for (int k = 0; k < words.length - 1; ++k) {
106+
t.add(words[k]);
107+
}
108+
for (String word : phrases[j].split(" ")) {
109+
t.add(word);
110+
}
111+
res.add(String.join(" ", t));
112+
}
113+
}
114+
}
115+
}
116+
List<String> output = new ArrayList<>(res);
117+
Collections.sort(output);
118+
return output;
119+
}
120+
}
74121
```
75122

76123
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public List<String> beforeAndAfterPuzzles(String[] phrases) {
3+
Map<String, Set<Integer>> sameFirstWord = new HashMap<>();
4+
for (int i = 0; i < phrases.length; ++i) {
5+
String phrase = phrases[i];
6+
String word = phrase.split(" ")[0];
7+
if (!sameFirstWord.containsKey(word)) {
8+
sameFirstWord.put(word, new HashSet<>());
9+
}
10+
sameFirstWord.get(word).add(i);
11+
}
12+
Set<String> res = new HashSet<>();
13+
for (int i = 0; i < phrases.length; ++i) {
14+
String phrase = phrases[i];
15+
String[] words = phrase.split(" ");
16+
String lastWord = words[words.length - 1];
17+
if (sameFirstWord.containsKey(lastWord)) {
18+
for (int j : sameFirstWord.get(lastWord)) {
19+
if (i != j) {
20+
List<String> t = new ArrayList<>();
21+
for (int k = 0; k < words.length - 1; ++k) {
22+
t.add(words[k]);
23+
}
24+
for (String word : phrases[j].split(" ")) {
25+
t.add(word);
26+
}
27+
res.add(String.join(" ", t));
28+
}
29+
}
30+
}
31+
}
32+
List<String> output = new ArrayList<>(res);
33+
Collections.sort(output);
34+
return output;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]:
3+
same_first_word = collections.defaultdict(set)
4+
for i, phrase in enumerate(phrases):
5+
same_first_word[phrase.split()[0]].add(i)
6+
res = set()
7+
for i, phrase in enumerate(phrases):
8+
words = phrase.split()
9+
last_word = words[-1]
10+
if last_word in same_first_word:
11+
for j in same_first_word[last_word]:
12+
if i != j:
13+
res.add(' '.join(words[:-1] + phrases[j].split()))
14+
return sorted(list(res))

0 commit comments

Comments
 (0)