File tree 6 files changed +176
-2
lines changed
solution/2000-2099/2068.Check Whether Two Strings are Almost Equivalent
6 files changed +176
-2
lines changed Original file line number Diff line number Diff line change 60
60
61
61
<!-- 这里可写通用的实现逻辑 -->
62
62
63
+ 哈希表计数。
64
+
63
65
<!-- tabs:start -->
64
66
65
67
### ** Python3**
66
68
67
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
70
69
71
``` 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())
71
80
```
72
81
73
82
### ** Java**
74
83
75
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
85
77
86
``` 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
+ ```
78
122
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
+ }
79
141
```
80
142
81
143
### ** ...**
Original file line number Diff line number Diff line change @@ -62,13 +62,73 @@ The difference is 4, which is more than the allowed 3.
62
62
### ** Python3**
63
63
64
64
``` 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())
66
73
```
67
74
68
75
### ** Java**
69
76
70
77
``` 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
+ ```
71
113
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
+ }
72
132
```
73
133
74
134
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 ())
You can’t perform that action at this time.
0 commit comments