File tree Expand file tree Collapse file tree 3 files changed +108
-0
lines changed
solution/0400-0499/0409.Longest Palindrome Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,25 @@ function longestPalindrome(s: string): number {
102
102
}
103
103
```
104
104
105
+ ``` ts
106
+ function longestPalindrome(s : string ): number {
107
+ const map = new Map ();
108
+ for (const c of s ) {
109
+ map .set (c , (map .get (c ) ?? 0 ) + 1 );
110
+ }
111
+ let hasOdd = false ;
112
+ let res = 0 ;
113
+ for (const v of map .values ()) {
114
+ res += v ;
115
+ if (v & 1 ) {
116
+ hasOdd = true ;
117
+ res -- ;
118
+ }
119
+ }
120
+ return res + (hasOdd ? 1 : 0 );
121
+ }
122
+ ```
123
+
105
124
### ** C++**
106
125
107
126
``` cpp
@@ -138,6 +157,31 @@ func longestPalindrome(s string) int {
138
157
}
139
158
```
140
159
160
+ ### ** Rust**
161
+
162
+ ``` rust
163
+ use std :: collections :: HashMap ;
164
+
165
+ impl Solution {
166
+ pub fn longest_palindrome (s : String ) -> i32 {
167
+ let mut map : HashMap <char , i32 > = HashMap :: new ();
168
+ for c in s . chars () {
169
+ map . insert (c , map . get (& c ). unwrap_or (& 0 ) + 1 );
170
+ }
171
+ let mut has_odd = false ;
172
+ let mut res = 0 ;
173
+ for v in map . values () {
174
+ res += v ;
175
+ if v % 2 == 1 {
176
+ has_odd = true ;
177
+ res -= 1 ;
178
+ }
179
+ }
180
+ res + if has_odd { 1 } else { 0 }
181
+ }
182
+ }
183
+ ```
184
+
141
185
### ** ...**
142
186
143
187
```
Original file line number Diff line number Diff line change @@ -92,6 +92,25 @@ function longestPalindrome(s: string): number {
92
92
}
93
93
```
94
94
95
+ ``` ts
96
+ function longestPalindrome(s : string ): number {
97
+ const map = new Map ();
98
+ for (const c of s ) {
99
+ map .set (c , (map .get (c ) ?? 0 ) + 1 );
100
+ }
101
+ let hasOdd = false ;
102
+ let res = 0 ;
103
+ for (const v of map .values ()) {
104
+ res += v ;
105
+ if (v & 1 ) {
106
+ hasOdd = true ;
107
+ res -- ;
108
+ }
109
+ }
110
+ return res + (hasOdd ? 1 : 0 );
111
+ }
112
+ ```
113
+
95
114
### ** C++**
96
115
97
116
``` cpp
@@ -128,6 +147,31 @@ func longestPalindrome(s string) int {
128
147
}
129
148
```
130
149
150
+ ### ** Rust**
151
+
152
+ ``` rust
153
+ use std :: collections :: HashMap ;
154
+
155
+ impl Solution {
156
+ pub fn longest_palindrome (s : String ) -> i32 {
157
+ let mut map : HashMap <char , i32 > = HashMap :: new ();
158
+ for c in s . chars () {
159
+ map . insert (c , map . get (& c ). unwrap_or (& 0 ) + 1 );
160
+ }
161
+ let mut has_odd = false ;
162
+ let mut res = 0 ;
163
+ for v in map . values () {
164
+ res += v ;
165
+ if v % 2 == 1 {
166
+ has_odd = true ;
167
+ res -= 1 ;
168
+ }
169
+ }
170
+ res + if has_odd { 1 } else { 0 }
171
+ }
172
+ }
173
+ ```
174
+
131
175
### ** ...**
132
176
133
177
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ impl Solution {
4
+ pub fn longest_palindrome ( s : String ) -> i32 {
5
+ let mut map: HashMap < char , i32 > = HashMap :: new ( ) ;
6
+ for c in s. chars ( ) {
7
+ map. insert ( c, map. get ( & c) . unwrap_or ( & 0 ) + 1 ) ;
8
+ }
9
+ let mut has_odd = false ;
10
+ let mut res = 0 ;
11
+ for v in map. values ( ) {
12
+ res += v;
13
+ if v % 2 == 1 {
14
+ has_odd = true ;
15
+ res -= 1 ;
16
+ }
17
+ }
18
+ res + if has_odd { 1 } else { 0 }
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments