Skip to content

Commit 8102809

Browse files
committed
feat: add solutions to lc problem: No.2068
No.2068.Check Whether Two Strings are Almost Equivalent
1 parent e7209ac commit 8102809

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

solution/2000-2099/2068.Check Whether Two Strings are Almost Equivalent/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,84 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
哈希表计数。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

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

6971
```python
70-
72+
class Solution:
73+
def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
74+
counter = defaultdict(int)
75+
for c in word1:
76+
counter[c] += 1
77+
for c in word2:
78+
counter[c] -= 1
79+
return all(abs(x) <= 3 for x in counter.values())
7180
```
7281

7382
### **Java**
7483

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

7786
```java
87+
class Solution {
88+
public boolean checkAlmostEquivalent(String word1, String word2) {
89+
int[] counter = new int[26];
90+
for (char c : word1.toCharArray()) {
91+
++counter[c - 'a'];
92+
}
93+
for (char c : word2.toCharArray()) {
94+
--counter[c - 'a'];
95+
}
96+
for (int i = 0; i < 26; ++i) {
97+
if (Math.abs(counter[i]) > 3) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
bool checkAlmostEquivalent(string word1, string word2) {
112+
vector<int> counter(26);
113+
for (char& c : word1) ++counter[c - 'a'];
114+
for (char& c : word2) --counter[c - 'a'];
115+
for (int i = 0; i < 26; ++i)
116+
if (abs(counter[i]) > 3)
117+
return 0;
118+
return 1;
119+
}
120+
};
121+
```
78122
123+
### **Go**
124+
125+
```go
126+
func checkAlmostEquivalent(word1 string, word2 string) bool {
127+
counter := make([]int, 26)
128+
for i := range word1 {
129+
counter[word1[i]-'a']++
130+
}
131+
for i := range word2 {
132+
counter[word2[i]-'a']--
133+
}
134+
for _, v := range counter {
135+
if v > 3 || -v > 3 {
136+
return false
137+
}
138+
}
139+
return true
140+
}
79141
```
80142

81143
### **...**

solution/2000-2099/2068.Check Whether Two Strings are Almost Equivalent/README_EN.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,73 @@ The difference is 4, which is more than the allowed 3.
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
67+
counter = defaultdict(int)
68+
for c in word1:
69+
counter[c] += 1
70+
for c in word2:
71+
counter[c] -= 1
72+
return all(abs(x) <= 3 for x in counter.values())
6673
```
6774

6875
### **Java**
6976

7077
```java
78+
class Solution {
79+
public boolean checkAlmostEquivalent(String word1, String word2) {
80+
int[] counter = new int[26];
81+
for (char c : word1.toCharArray()) {
82+
++counter[c - 'a'];
83+
}
84+
for (char c : word2.toCharArray()) {
85+
--counter[c - 'a'];
86+
}
87+
for (int i = 0; i < 26; ++i) {
88+
if (Math.abs(counter[i]) > 3) {
89+
return false;
90+
}
91+
}
92+
return true;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool checkAlmostEquivalent(string word1, string word2) {
103+
vector<int> counter(26);
104+
for (char& c : word1) ++counter[c - 'a'];
105+
for (char& c : word2) --counter[c - 'a'];
106+
for (int i = 0; i < 26; ++i)
107+
if (abs(counter[i]) > 3)
108+
return 0;
109+
return 1;
110+
}
111+
};
112+
```
71113
114+
### **Go**
115+
116+
```go
117+
func checkAlmostEquivalent(word1 string, word2 string) bool {
118+
counter := make([]int, 26)
119+
for i := range word1 {
120+
counter[word1[i]-'a']++
121+
}
122+
for i := range word2 {
123+
counter[word2[i]-'a']--
124+
}
125+
for _, v := range counter {
126+
if v > 3 || -v > 3 {
127+
return false
128+
}
129+
}
130+
return true
131+
}
72132
```
73133

74134
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool checkAlmostEquivalent(string word1, string word2) {
4+
vector<int> counter(26);
5+
for (char& c : word1) ++counter[c - 'a'];
6+
for (char& c : word2) --counter[c - 'a'];
7+
for (int i = 0; i < 26; ++i)
8+
if (abs(counter[i]) > 3)
9+
return 0;
10+
return 1;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func checkAlmostEquivalent(word1 string, word2 string) bool {
2+
counter := make([]int, 26)
3+
for i := range word1 {
4+
counter[word1[i]-'a']++
5+
}
6+
for i := range word2 {
7+
counter[word2[i]-'a']--
8+
}
9+
for _, v := range counter {
10+
if v > 3 || -v > 3 {
11+
return false
12+
}
13+
}
14+
return true
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean checkAlmostEquivalent(String word1, String word2) {
3+
int[] counter = new int[26];
4+
for (char c : word1.toCharArray()) {
5+
++counter[c - 'a'];
6+
}
7+
for (char c : word2.toCharArray()) {
8+
--counter[c - 'a'];
9+
}
10+
for (int i = 0; i < 26; ++i) {
11+
if (Math.abs(counter[i]) > 3) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
3+
counter = defaultdict(int)
4+
for c in word1:
5+
counter[c] += 1
6+
for c in word2:
7+
counter[c] -= 1
8+
return all(abs(x) <= 3 for x in counter.values())

0 commit comments

Comments
 (0)