Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07b2d98

Browse files
feat: update solutions to lc problem: No.2744 (doocs#2228)
No.2744.Find Maximum Number of String Pairs
1 parent 5d8baa3 commit 07b2d98

File tree

7 files changed

+64
-70
lines changed

7 files changed

+64
-70
lines changed

‎solution/2700-2799/2744.Find Maximum Number of String Pairs/README.md‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@
6767

6868
我们可以用哈希表 $cnt$ 来存储数组 $words$ 中每个字符串的反转字符串出现的次数。
6969

70-
遍历数组 $words,ドル对于每个字符串 $w,ドル我们直接将 $cnt[w]$ 的值加到答案中,然后将 $w$ 的反转字符串出现的次数加 1ドル$。
70+
遍历数组 $words,ドル对于每个字符串 $w,ドル我们将其反转字符串 $w$ 的出现次数加到答案中,然后将 $w$ 的出现次数加 1ドル$。
7171

72-
遍历结束后,即可得到最大匹配数目
72+
最后返回答案
7373

74-
时间复杂度 $O(L),ドル空间复杂度 $O(L),ドル其中 $L$ 是数组 $words$ 中字符串的长度之和
74+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $words$ 的长度
7575

7676
<!-- tabs:start -->
7777

@@ -81,19 +81,20 @@ class Solution:
8181
cnt = Counter()
8282
ans = 0
8383
for w in words:
84-
ans += cnt[w]
85-
cnt[w[::-1]] += 1
84+
ans += cnt[w[::-1]]
85+
cnt[w] += 1
8686
return ans
8787
```
8888

8989
```java
9090
class Solution {
9191
public int maximumNumberOfStringPairs(String[] words) {
92-
Map<String, Integer> cnt = new HashMap<>(words.length);
92+
Map<Integer, Integer> cnt = new HashMap<>();
9393
int ans = 0;
94-
for (String w : words) {
95-
ans += cnt.getOrDefault(w, 0);
96-
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
94+
for (var w : words) {
95+
int a = w.charAt(0) - 'a', b = w.charAt(1) - 'a';
96+
ans += cnt.getOrDefault(b << 5 | a, 0);
97+
cnt.merge(a << 5 | b, 1, Integer::sum);
9798
}
9899
return ans;
99100
}
@@ -104,12 +105,12 @@ class Solution {
104105
class Solution {
105106
public:
106107
int maximumNumberOfStringPairs(vector<string>& words) {
107-
unordered_map<string, int> cnt;
108+
unordered_map<int, int> cnt;
108109
int ans = 0;
109110
for (auto& w : words) {
110-
ans += cnt[w];
111-
reverse(w.begin(), w.end());
112-
cnt[w]++;
111+
int a = w[0] - 'a', b = w[1] - 'a';
112+
ans += cnt[b << 5 | a];
113+
cnt[a << 5 | b]++;
113114
}
114115
return ans;
115116
}
@@ -118,27 +119,24 @@ public:
118119
119120
```go
120121
func maximumNumberOfStringPairs(words []string) (ans int) {
121-
cnt := map[string]int{}
122+
cnt := map[int]int{}
122123
for _, w := range words {
123-
ans += cnt[w]
124-
s := []byte(w)
125-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
126-
s[i], s[j] = s[j], s[i]
127-
}
128-
cnt[string(s)]++
124+
a, b := int(w[0]-'a'), int(w[1]-'a')
125+
ans += cnt[b<<5|a]
126+
cnt[a<<5|b]++
129127
}
130128
return
131129
}
132130
```
133131

134132
```ts
135133
function maximumNumberOfStringPairs(words: string[]): number {
136-
const cnt: Map<string, number> =newMap();
134+
const cnt: { [key:number]:number } = {};
137135
let ans = 0;
138136
for (const w of words) {
139-
ans+=cnt.get(w) ||0;
140-
const s =w.split('').reverse().join('');
141-
cnt.set(s, (cnt.get(s) || 0) + 1);
137+
const [a, b] = [w.charCodeAt(0) -97, w.charCodeAt(w.length-1) -97];
138+
ans+=cnt[(b<<5) |a] ||0;
139+
cnt[(a<<5) |b] =(cnt[(a<<5) |b] || 0) + 1;
142140
}
143141
return ans;
144142
}

‎solution/2700-2799/2744.Find Maximum Number of String Pairs/README_EN.md‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ It can be proven that 1 is the maximum number of pairs that can be formed.
6060

6161
### Solution 1: Hash Table
6262

63-
We can use a hash table $cnt$ to store the number of occurrences of each string's reversed string in the array $words$.
63+
We can use a hash table $cnt$ to store the number of occurrences of each reversed string in the array $words$.
6464

65-
Traverse the array $words$, for each string $w,ドル we directly add the value of $cnt[w]$ to the answer, then increase the occurrence count of $w$'s reversed string by 1ドル$.
65+
We iterate through the array $words$. For each string $w,ドル we add the number of occurrences of its reversed string to the answer, then increment the count of $w$ by 1ドル$.
6666

67-
After the traversal ends, we can get the maximum number of matches.
67+
Finally, we return the answer.
6868

69-
The time complexity is $O(L),ドル and the space complexity is $O(L),ドル where $L$ is the sum of the lengths of the strings in the array $words$.
69+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $words$.
7070

7171
<!-- tabs:start -->
7272

@@ -76,19 +76,20 @@ class Solution:
7676
cnt = Counter()
7777
ans = 0
7878
for w in words:
79-
ans += cnt[w]
80-
cnt[w[::-1]] += 1
79+
ans += cnt[w[::-1]]
80+
cnt[w] += 1
8181
return ans
8282
```
8383

8484
```java
8585
class Solution {
8686
public int maximumNumberOfStringPairs(String[] words) {
87-
Map<String, Integer> cnt = new HashMap<>(words.length);
87+
Map<Integer, Integer> cnt = new HashMap<>();
8888
int ans = 0;
89-
for (String w : words) {
90-
ans += cnt.getOrDefault(w, 0);
91-
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
89+
for (var w : words) {
90+
int a = w.charAt(0) - 'a', b = w.charAt(1) - 'a';
91+
ans += cnt.getOrDefault(b << 5 | a, 0);
92+
cnt.merge(a << 5 | b, 1, Integer::sum);
9293
}
9394
return ans;
9495
}
@@ -99,12 +100,12 @@ class Solution {
99100
class Solution {
100101
public:
101102
int maximumNumberOfStringPairs(vector<string>& words) {
102-
unordered_map<string, int> cnt;
103+
unordered_map<int, int> cnt;
103104
int ans = 0;
104105
for (auto& w : words) {
105-
ans += cnt[w];
106-
reverse(w.begin(), w.end());
107-
cnt[w]++;
106+
int a = w[0] - 'a', b = w[1] - 'a';
107+
ans += cnt[b << 5 | a];
108+
cnt[a << 5 | b]++;
108109
}
109110
return ans;
110111
}
@@ -113,27 +114,24 @@ public:
113114
114115
```go
115116
func maximumNumberOfStringPairs(words []string) (ans int) {
116-
cnt := map[string]int{}
117+
cnt := map[int]int{}
117118
for _, w := range words {
118-
ans += cnt[w]
119-
s := []byte(w)
120-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
121-
s[i], s[j] = s[j], s[i]
122-
}
123-
cnt[string(s)]++
119+
a, b := int(w[0]-'a'), int(w[1]-'a')
120+
ans += cnt[b<<5|a]
121+
cnt[a<<5|b]++
124122
}
125123
return
126124
}
127125
```
128126

129127
```ts
130128
function maximumNumberOfStringPairs(words: string[]): number {
131-
const cnt: Map<string, number> =newMap();
129+
const cnt: { [key:number]:number } = {};
132130
let ans = 0;
133131
for (const w of words) {
134-
ans+=cnt.get(w) ||0;
135-
const s =w.split('').reverse().join('');
136-
cnt.set(s, (cnt.get(s) || 0) + 1);
132+
const [a, b] = [w.charCodeAt(0) -97, w.charCodeAt(w.length-1) -97];
133+
ans+=cnt[(b<<5) |a] ||0;
134+
cnt[(a<<5) |b] =(cnt[(a<<5) |b] || 0) + 1;
137135
}
138136
return ans;
139137
}

‎solution/2700-2799/2744.Find Maximum Number of String Pairs/Solution.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int maximumNumberOfStringPairs(vector<string>& words) {
4-
unordered_map<string, int> cnt;
4+
unordered_map<int, int> cnt;
55
int ans = 0;
66
for (auto& w : words) {
7-
ans += cnt[w];
8-
reverse(w.begin(), w.end());
9-
cnt[w]++;
7+
int a = w[0] - 'a', b = w[1] - 'a';
8+
ans += cnt[b << 5 | a];
9+
cnt[a << 5 | b]++;
1010
}
1111
return ans;
1212
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
func maximumNumberOfStringPairs(words []string) (ans int) {
2-
cnt := map[string]int{}
2+
cnt := map[int]int{}
33
for _, w := range words {
4-
ans += cnt[w]
5-
s := []byte(w)
6-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
7-
s[i], s[j] = s[j], s[i]
8-
}
9-
cnt[string(s)]++
4+
a, b := int(w[0]-'a'), int(w[1]-'a')
5+
ans += cnt[b<<5|a]
6+
cnt[a<<5|b]++
107
}
118
return
129
}

‎solution/2700-2799/2744.Find Maximum Number of String Pairs/Solution.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution {
22
public int maximumNumberOfStringPairs(String[] words) {
3-
Map<String, Integer> cnt = new HashMap<>(words.length);
3+
Map<Integer, Integer> cnt = new HashMap<>();
44
int ans = 0;
5-
for (String w : words) {
6-
ans += cnt.getOrDefault(w, 0);
7-
cnt.merge(new StringBuilder(w).reverse().toString(), 1, Integer::sum);
5+
for (var w : words) {
6+
int a = w.charAt(0) - 'a', b = w.charAt(1) - 'a';
7+
ans += cnt.getOrDefault(b << 5 | a, 0);
8+
cnt.merge(a << 5 | b, 1, Integer::sum);
89
}
910
return ans;
1011
}

‎solution/2700-2799/2744.Find Maximum Number of String Pairs/Solution.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ def maximumNumberOfStringPairs(self, words: List[str]) -> int:
33
cnt = Counter()
44
ans = 0
55
for w in words:
6-
ans += cnt[w]
7-
cnt[w[::-1]] += 1
6+
ans += cnt[w[::-1]]
7+
cnt[w] += 1
88
return ans
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function maximumNumberOfStringPairs(words: string[]): number {
2-
const cnt: Map<string,number>=newMap();
2+
const cnt: {[key: number]: number}={};
33
let ans = 0;
44
for (const w of words) {
5-
ans+=cnt.get(w)||0;
6-
consts=w.split('').reverse().join('');
7-
cnt.set(s,(cnt.get(s) || 0) + 1);
5+
const[a,b]=[w.charCodeAt(0)-97,w.charCodeAt(w.length-1)-97];
6+
ans+=cnt[(b<<5)|a]||0;
7+
cnt[(a<<5)|b]=(cnt[(a<<5) |b]|| 0) + 1;
88
}
99
return ans;
1010
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /