File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
solution/0400-0499/0409.Longest Palindrome Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -183,4 +183,30 @@ impl Solution {
183
183
184
184
<!-- solution: end -->
185
185
186
+ <!-- solution: start -->
187
+
188
+ ### Solution 2: Bitwise inversion and odd counting
189
+
190
+ <!-- tabs: start -->
191
+
192
+ #### TypeScript
193
+
194
+ ``` ts
195
+ function longestPalindrome(s : string ): number {
196
+ const odd: Record <string , number > = {};
197
+ let c = 0 ;
198
+
199
+ for (const ch of s ) {
200
+ odd [ch ] ^= 1 ;
201
+ c += odd [ch ] ? 1 : - 1 ;
202
+ }
203
+
204
+ return c ? s .length - c + 1 : s .length ;
205
+ }
206
+ ```
207
+
208
+ <!-- tabs: end -->
209
+
210
+ <!-- solution: end -->
211
+
186
212
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -180,4 +180,30 @@ impl Solution {
180
180
181
181
<!-- solution: end -->
182
182
183
+ <!-- solution: start -->
184
+
185
+ ### Solution 2: Bitwise inversion and odd counting
186
+
187
+ <!-- tabs: start -->
188
+
189
+ #### TypeScript
190
+
191
+ ``` ts
192
+ function longestPalindrome(s : string ): number {
193
+ const odd: Record <string , number > = {};
194
+ let c = 0 ;
195
+
196
+ for (const ch of s ) {
197
+ odd [ch ] ^= 1 ;
198
+ c += odd [ch ] ? 1 : - 1 ;
199
+ }
200
+
201
+ return c ? s .length - c + 1 : s .length ;
202
+ }
203
+ ```
204
+
205
+ <!-- tabs: end -->
206
+
207
+ <!-- solution: end -->
208
+
183
209
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function longestPalindrome ( s : string ) : number {
2
+ const odd : Record < string , number > = { } ;
3
+ let c = 0 ;
4
+
5
+ for ( const ch of s ) {
6
+ odd [ ch ] ^= 1 ;
7
+ c += odd [ ch ] ? 1 : - 1 ;
8
+ }
9
+
10
+ return c ? s . length - c + 1 : s . length ;
11
+ }
You can’t perform that action at this time.
0 commit comments