67
67
68
68
<!-- 这里可写通用的实现逻辑 -->
69
69
70
+ ** 方法一:计数**
71
+
72
+ 我们先判断字符串 $s$ 的长度是否小于 $k$,如果是,那么一定无法构造出 $k$ 个回文串,可以直接返回 ` false ` 。
73
+
74
+ 否则,我们用一个哈希表或数组 $cnt$ 统计字符串 $s$ 中每个字符出现的次数。最后,我们只需要统计 $cnt$ 中奇数次数的字符个数 $x$,如果 $x$ 大于 $k$,那么一定无法构造出 $k$ 个回文串,返回 ` false ` ;否则,返回 ` true ` 。
75
+
76
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,这里 $C=26$。
77
+
70
78
<!-- tabs:start -->
71
79
72
80
### ** Python3**
@@ -78,9 +86,8 @@ class Solution:
78
86
def canConstruct (self , s : str , k : int ) -> bool :
79
87
if len (s) < k:
80
88
return False
81
- counter = Counter(s)
82
- cnt = sum (1 for n in counter.values() if n % 2 == 1 )
83
- return cnt <= k
89
+ cnt = Counter(s)
90
+ return sum (v & 1 for v in cnt.values()) <= k
84
91
```
85
92
86
93
### ** Java**
@@ -90,20 +97,19 @@ class Solution:
90
97
``` java
91
98
class Solution {
92
99
public boolean canConstruct (String s , int k ) {
93
- if (s. length() < k) {
100
+ int n = s. length();
101
+ if (n < k) {
94
102
return false ;
95
103
}
96
- int [] counter = new int [26 ];
97
- for (char c : s . toCharArray() ) {
98
- ++ counter[c - ' a' ];
104
+ int [] cnt = new int [26 ];
105
+ for (int i = 0 ; i < n; ++ i ) {
106
+ ++ cnt[s . charAt(i) - ' a' ];
99
107
}
100
- int cnt = 0 ;
101
- for (int v : counter) {
102
- if (v % 2 == 1 ) {
103
- ++ cnt;
104
- }
108
+ int x = 0 ;
109
+ for (int v : cnt) {
110
+ x += v & 1 ;
105
111
}
106
- return cnt <= k;
112
+ return x <= k;
107
113
}
108
114
}
109
115
```
@@ -114,14 +120,18 @@ class Solution {
114
120
class Solution {
115
121
public:
116
122
bool canConstruct(string s, int k) {
117
- if (s.size() < k) return 0;
118
- vector<int > counter(26);
119
- for (char c : s) ++counter[ c - 'a'] ;
120
- int cnt = 0;
121
- for (int v : counter)
122
- if (v % 2)
123
- ++cnt;
124
- return cnt <= k;
123
+ if (s.size() < k) {
124
+ return false;
125
+ }
126
+ int cnt[ 26] {};
127
+ for (char& c : s) {
128
+ ++cnt[ c - 'a'] ;
129
+ }
130
+ int x = 0;
131
+ for (int v : cnt) {
132
+ x += v & 1;
133
+ }
134
+ return x <= k;
125
135
}
126
136
};
127
137
```
@@ -133,17 +143,34 @@ func canConstruct(s string, k int) bool {
133
143
if len(s) < k {
134
144
return false
135
145
}
136
- counter := make([ ]int, 26)
146
+ cnt := [26 ]int{}
137
147
for _, c := range s {
138
- counter [c-'a']++
148
+ cnt [c-'a']++
139
149
}
140
- cnt := 0
141
- for _, v := range counter {
142
- if v%2 == 1 {
143
- cnt++
144
- }
150
+ x := 0
151
+ for _, v := range cnt {
152
+ x += v & 1
145
153
}
146
- return cnt <= k
154
+ return x <= k
155
+ }
156
+ ```
157
+
158
+ ### ** TypeScript**
159
+
160
+ ``` ts
161
+ function canConstruct(s : string , k : number ): boolean {
162
+ if (s .length < k ) {
163
+ return false ;
164
+ }
165
+ const cnt: number [] = new Array (26 ).fill (0 );
166
+ for (const c of s ) {
167
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
168
+ }
169
+ let x = 0 ;
170
+ for (const v of cnt ) {
171
+ x += v & 1 ;
172
+ }
173
+ return x <= k ;
147
174
}
148
175
```
149
176
0 commit comments