Skip to content

Commit 0a9776d

Browse files
committed
feat: add solutions to lc problem: No.2135
No.2135.Count Words Obtained After Adding a Letter
1 parent fb34383 commit 0a9776d

File tree

6 files changed

+328
-2
lines changed

6 files changed

+328
-2
lines changed

solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,127 @@
7575
<!-- 这里可写当前语言的特殊实现逻辑 -->
7676

7777
```python
78-
78+
class Solution:
79+
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
80+
s = set()
81+
for word in startWords:
82+
mask = 0
83+
for c in word:
84+
mask |= (1 << (ord(c) - ord('a')))
85+
s.add(mask)
86+
87+
ans = 0
88+
for word in targetWords:
89+
mask = 0
90+
for c in word:
91+
mask |= (1 << (ord(c) - ord('a')))
92+
for c in word:
93+
t = mask ^ (1 << (ord(c) - ord('a')))
94+
if t in s:
95+
ans += 1
96+
break
97+
return ans
7998
```
8099

81100
### **Java**
82101

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

85104
```java
105+
class Solution {
106+
107+
public int wordCount(String[] startWords, String[] targetWords) {
108+
Set<Integer> s = new HashSet<>();
109+
for (String word : startWords) {
110+
int mask = 0;
111+
for (char c : word.toCharArray()) {
112+
mask |= (1 << (c - 'a'));
113+
}
114+
s.add(mask);
115+
}
116+
int ans = 0;
117+
for (String word : targetWords) {
118+
int mask = 0;
119+
for (char c : word.toCharArray()) {
120+
mask |= (1 << (c - 'a'));
121+
}
122+
for (char c : word.toCharArray()) {
123+
int t = mask ^ (1 << (c - 'a'));
124+
if (s.contains(t)) {
125+
++ans;
126+
break;
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int wordCount(vector<string>& startWords, vector<string>& targetWords) {
142+
unordered_set<int> s;
143+
for (auto& word : startWords)
144+
{
145+
int mask = 0;
146+
for (char c : word)
147+
mask |= (1 << (c - 'a'));
148+
s.insert(mask);
149+
}
150+
int ans = 0;
151+
for (auto& word : targetWords)
152+
{
153+
int mask = 0;
154+
for (char c : word)
155+
mask |= (1 << (c - 'a'));
156+
for (char c : word)
157+
{
158+
int t = mask ^ (1 << (c - 'a'));
159+
if (s.count(t))
160+
{
161+
++ans;
162+
break;
163+
}
164+
}
165+
}
166+
return ans;
167+
}
168+
};
169+
```
86170
171+
### **Go**
172+
173+
```go
174+
func wordCount(startWords []string, targetWords []string) int {
175+
s := make(map[int]bool)
176+
for _, word := range startWords {
177+
mask := 0
178+
for _, c := range word {
179+
mask |= (1 << (c - 'a'))
180+
}
181+
s[mask] = true
182+
}
183+
ans := 0
184+
for _, word := range targetWords {
185+
mask := 0
186+
for _, c := range word {
187+
mask |= (1 << (c - 'a'))
188+
}
189+
for _, c := range word {
190+
t := mask ^ (1 << (c - 'a'))
191+
if s[t] {
192+
ans++
193+
break
194+
}
195+
}
196+
}
197+
return ans
198+
}
87199
```
88200

89201
### **TypeScript**

solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README_EN.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,125 @@
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
72+
s = set()
73+
for word in startWords:
74+
mask = 0
75+
for c in word:
76+
mask |= (1 << (ord(c) - ord('a')))
77+
s.add(mask)
78+
79+
ans = 0
80+
for word in targetWords:
81+
mask = 0
82+
for c in word:
83+
mask |= (1 << (ord(c) - ord('a')))
84+
for c in word:
85+
t = mask ^ (1 << (ord(c) - ord('a')))
86+
if t in s:
87+
ans += 1
88+
break
89+
return ans
7190
```
7291

7392
### **Java**
7493

7594
```java
95+
class Solution {
96+
97+
public int wordCount(String[] startWords, String[] targetWords) {
98+
Set<Integer> s = new HashSet<>();
99+
for (String word : startWords) {
100+
int mask = 0;
101+
for (char c : word.toCharArray()) {
102+
mask |= (1 << (c - 'a'));
103+
}
104+
s.add(mask);
105+
}
106+
int ans = 0;
107+
for (String word : targetWords) {
108+
int mask = 0;
109+
for (char c : word.toCharArray()) {
110+
mask |= (1 << (c - 'a'));
111+
}
112+
for (char c : word.toCharArray()) {
113+
int t = mask ^ (1 << (c - 'a'));
114+
if (s.contains(t)) {
115+
++ans;
116+
break;
117+
}
118+
}
119+
}
120+
return ans;
121+
}
122+
}
123+
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
int wordCount(vector<string>& startWords, vector<string>& targetWords) {
132+
unordered_set<int> s;
133+
for (auto& word : startWords)
134+
{
135+
int mask = 0;
136+
for (char c : word)
137+
mask |= (1 << (c - 'a'));
138+
s.insert(mask);
139+
}
140+
int ans = 0;
141+
for (auto& word : targetWords)
142+
{
143+
int mask = 0;
144+
for (char c : word)
145+
mask |= (1 << (c - 'a'));
146+
for (char c : word)
147+
{
148+
int t = mask ^ (1 << (c - 'a'));
149+
if (s.count(t))
150+
{
151+
++ans;
152+
break;
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
};
159+
```
76160
161+
### **Go**
162+
163+
```go
164+
func wordCount(startWords []string, targetWords []string) int {
165+
s := make(map[int]bool)
166+
for _, word := range startWords {
167+
mask := 0
168+
for _, c := range word {
169+
mask |= (1 << (c - 'a'))
170+
}
171+
s[mask] = true
172+
}
173+
ans := 0
174+
for _, word := range targetWords {
175+
mask := 0
176+
for _, c := range word {
177+
mask |= (1 << (c - 'a'))
178+
}
179+
for _, c := range word {
180+
t := mask ^ (1 << (c - 'a'))
181+
if s[t] {
182+
ans++
183+
break
184+
}
185+
}
186+
}
187+
return ans
188+
}
77189
```
78190

79191
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int wordCount(vector<string>& startWords, vector<string>& targetWords) {
4+
unordered_set<int> s;
5+
for (auto& word : startWords)
6+
{
7+
int mask = 0;
8+
for (char c : word)
9+
mask |= (1 << (c - 'a'));
10+
s.insert(mask);
11+
}
12+
int ans = 0;
13+
for (auto& word : targetWords)
14+
{
15+
int mask = 0;
16+
for (char c : word)
17+
mask |= (1 << (c - 'a'));
18+
for (char c : word)
19+
{
20+
int t = mask ^ (1 << (c - 'a'));
21+
if (s.count(t))
22+
{
23+
++ans;
24+
break;
25+
}
26+
}
27+
}
28+
return ans;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func wordCount(startWords []string, targetWords []string) int {
2+
s := make(map[int]bool)
3+
for _, word := range startWords {
4+
mask := 0
5+
for _, c := range word {
6+
mask |= (1 << (c - 'a'))
7+
}
8+
s[mask] = true
9+
}
10+
ans := 0
11+
for _, word := range targetWords {
12+
mask := 0
13+
for _, c := range word {
14+
mask |= (1 << (c - 'a'))
15+
}
16+
for _, c := range word {
17+
t := mask ^ (1 << (c - 'a'))
18+
if s[t] {
19+
ans++
20+
break
21+
}
22+
}
23+
}
24+
return ans
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int wordCount(String[] startWords, String[] targetWords) {
3+
Set<Integer> s = new HashSet<>();
4+
for (String word : startWords) {
5+
int mask = 0;
6+
for (char c : word.toCharArray()) {
7+
mask |= (1 << (c - 'a'));
8+
}
9+
s.add(mask);
10+
}
11+
int ans = 0;
12+
for (String word : targetWords) {
13+
int mask = 0;
14+
for (char c : word.toCharArray()) {
15+
mask |= (1 << (c - 'a'));
16+
}
17+
for (char c : word.toCharArray()) {
18+
int t = mask ^ (1 << (c - 'a'));
19+
if (s.contains(t)) {
20+
++ans;
21+
break;
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
3+
s = set()
4+
for word in startWords:
5+
mask = 0
6+
for c in word:
7+
mask |= (1 << (ord(c) - ord('a')))
8+
s.add(mask)
9+
10+
ans = 0
11+
for word in targetWords:
12+
mask = 0
13+
for c in word:
14+
mask |= (1 << (ord(c) - ord('a')))
15+
for c in word:
16+
t = mask ^ (1 << (ord(c) - ord('a')))
17+
if t in s:
18+
ans += 1
19+
break
20+
return ans

0 commit comments

Comments
 (0)