File tree 6 files changed +164
-8
lines changed
solution/2500-2599/2506.Count Pairs Of Similar Strings
6 files changed +164
-8
lines changed Original file line number Diff line number Diff line change 58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
61
+ ** 方法一:哈希表 + 位运算**
62
+
63
+ 对于每个字符串,我们可以将其转换为一个长度为 $26$ 的二进制数,其中第 $i$ 位为 $1$ 表示该字符串中包含第 $i$ 个字母。
64
+
65
+ 如果两个字符串包含相同的字母,则它们的二进制数是相同的,因此,对于每个字符串,我们用哈希表统计其二进制数出现的次数,每一次累加到答案中,再将其二进制数出现的次数加 $1$。
66
+
67
+ 时间复杂度 $O(L)$,空间复杂度 $O(n)$。其中 $L$ 是所有字符串的长度之和,而 $n$ 是字符串的数量。
68
+
61
69
<!-- tabs:start -->
62
70
63
71
### ** Python3**
64
72
65
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
74
67
75
``` python
68
-
76
+ class Solution :
77
+ def similarPairs (self , words : List[str ]) -> int :
78
+ ans = 0
79
+ cnt = Counter()
80
+ for w in words:
81
+ v = 0
82
+ for c in w:
83
+ v |= 1 << (ord (c) - ord (" A" ))
84
+ ans += cnt[v]
85
+ cnt[v] += 1
86
+ return ans
69
87
```
70
88
71
89
### ** Java**
72
90
73
91
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
92
75
93
``` java
76
-
94
+ class Solution {
95
+ public int similarPairs (String [] words ) {
96
+ int ans = 0 ;
97
+ Map<Integer , Integer > cnt = new HashMap<> ();
98
+ for (var w : words) {
99
+ int v = 0 ;
100
+ for (int i = 0 ; i < w. length(); ++ i) {
101
+ v |= 1 << (w. charAt(i) - ' a' );
102
+ }
103
+ ans += cnt. getOrDefault(v, 0 );
104
+ cnt. put(v, cnt. getOrDefault(v, 0 ) + 1 );
105
+ }
106
+ return ans;
107
+ }
108
+ }
77
109
```
78
110
79
111
### ** C++**
80
112
81
113
``` cpp
82
-
114
+ class Solution {
115
+ public:
116
+ int similarPairs(vector<string >& words) {
117
+ int ans = 0;
118
+ unordered_map<int, int> cnt;
119
+ for (auto& w : words) {
120
+ int v = 0;
121
+ for (auto& c : w) v |= 1 << c - 'a';
122
+ ans += cnt[ v] ;
123
+ cnt[ v] ++;
124
+ }
125
+ return ans;
126
+ }
127
+ };
83
128
```
84
129
85
130
### **Go**
86
131
87
132
```go
88
-
133
+ func similarPairs(words []string) (ans int) {
134
+ cnt := map[int]int{}
135
+ for _, w := range words {
136
+ v := 0
137
+ for _, c := range w {
138
+ v |= 1 << (c - 'a')
139
+ }
140
+ ans += cnt[v]
141
+ cnt[v]++
142
+ }
143
+ return
144
+ }
89
145
```
90
146
91
147
### ** ...**
Original file line number Diff line number Diff line change 60
60
### ** Python3**
61
61
62
62
``` python
63
-
63
+ class Solution :
64
+ def similarPairs (self , words : List[str ]) -> int :
65
+ ans = 0
66
+ cnt = Counter()
67
+ for w in words:
68
+ v = 0
69
+ for c in w:
70
+ v |= 1 << (ord (c) - ord (" A" ))
71
+ ans += cnt[v]
72
+ cnt[v] += 1
73
+ return ans
64
74
```
65
75
66
76
### ** Java**
67
77
68
78
``` java
69
-
79
+ class Solution {
80
+ public int similarPairs (String [] words ) {
81
+ int ans = 0 ;
82
+ Map<Integer , Integer > cnt = new HashMap<> ();
83
+ for (var w : words) {
84
+ int v = 0 ;
85
+ for (int i = 0 ; i < w. length(); ++ i) {
86
+ v |= 1 << (w. charAt(i) - ' a' );
87
+ }
88
+ ans += cnt. getOrDefault(v, 0 );
89
+ cnt. put(v, cnt. getOrDefault(v, 0 ) + 1 );
90
+ }
91
+ return ans;
92
+ }
93
+ }
70
94
```
71
95
72
96
### ** C++**
73
97
74
98
``` cpp
75
-
99
+ class Solution {
100
+ public:
101
+ int similarPairs(vector<string >& words) {
102
+ int ans = 0;
103
+ unordered_map<int, int> cnt;
104
+ for (auto& w : words) {
105
+ int v = 0;
106
+ for (auto& c : w) v |= 1 << c - 'a';
107
+ ans += cnt[ v] ;
108
+ cnt[ v] ++;
109
+ }
110
+ return ans;
111
+ }
112
+ };
76
113
```
77
114
78
115
### **Go**
79
116
80
117
```go
81
-
118
+ func similarPairs(words []string) (ans int) {
119
+ cnt := map[int]int{}
120
+ for _, w := range words {
121
+ v := 0
122
+ for _, c := range w {
123
+ v |= 1 << (c - 'a')
124
+ }
125
+ ans += cnt[v]
126
+ cnt[v]++
127
+ }
128
+ return
129
+ }
82
130
```
83
131
84
132
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int similarPairs (vector<string>& words) {
4
+ int ans = 0 ;
5
+ unordered_map<int , int > cnt;
6
+ for (auto & w : words) {
7
+ int v = 0 ;
8
+ for (auto & c : w) v |= 1 << c - ' a' ;
9
+ ans += cnt[v];
10
+ cnt[v]++;
11
+ }
12
+ return ans;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ func similarPairs (words []string ) (ans int ) {
2
+ cnt := map [int ]int {}
3
+ for _ , w := range words {
4
+ v := 0
5
+ for _ , c := range w {
6
+ v |= 1 << (c - 'a' )
7
+ }
8
+ ans += cnt [v ]
9
+ cnt [v ]++
10
+ }
11
+ return
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int similarPairs (String [] words ) {
3
+ int ans = 0 ;
4
+ Map <Integer , Integer > cnt = new HashMap <>();
5
+ for (var w : words ) {
6
+ int v = 0 ;
7
+ for (int i = 0 ; i < w .length (); ++i ) {
8
+ v |= 1 << (w .charAt (i ) - 'a' );
9
+ }
10
+ ans += cnt .getOrDefault (v , 0 );
11
+ cnt .put (v , cnt .getOrDefault (v , 0 ) + 1 );
12
+ }
13
+ return ans ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def similarPairs (self , words : List [str ]) -> int :
3
+ ans = 0
4
+ cnt = Counter ()
5
+ for w in words :
6
+ v = 0
7
+ for c in w :
8
+ v |= 1 << (ord (c ) - ord ("A" ))
9
+ ans += cnt [v ]
10
+ cnt [v ] += 1
11
+ return ans
You can’t perform that action at this time.
0 commit comments