comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
Easy |
|
Given a string s
which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa"
is not considered a palindrome.
Example 1:
Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000
s
consists of lowercase and/or uppercase English letters only.
A valid palindrome string can have at most one character that appears an odd number of times, and the rest of the characters appear an even number of times.
Therefore, we can first traverse the string
Then, we traverse
Finally, if the answer is less than the length of the string
The time complexity is
class Solution:
def longestPalindrome(self, s: str) -> int:
cnt = Counter(s)
ans = sum(v // 2 * 2 for v in cnt.values())
ans += int(ans < len(s))
return ans
class Solution {
public int longestPalindrome(String s) {
int[] cnt = new int[128];
int n = s.length();
for (int i = 0; i < n; ++i) {
++cnt[s.charAt(i)];
}
int ans = 0;
for (int v : cnt) {
ans += v / 2 * 2;
}
ans += ans < n ? 1 : 0;
return ans;
}
}
class Solution {
public:
int longestPalindrome(string s) {
int cnt[128]{};
for (char c : s) {
++cnt[c];
}
int ans = 0;
for (int v : cnt) {
ans += v / 2 * 2;
}
ans += ans < s.size();
return ans;
}
};
func longestPalindrome(s string) (ans int) {
cnt := [128]int{}
for _, c := range s {
cnt[c]++
}
for _, v := range cnt {
ans += v / 2 * 2
}
if ans < len(s) {
ans++
}
return
}
function longestPalindrome(s: string): number {
const cnt: Record<string, number> = {};
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
}
let ans = Object.values(cnt).reduce((acc, v) => acc + Math.floor(v / 2) * 2, 0);
ans += ans < s.length ? 1 : 0;
return ans;
}
use std::collections::HashMap;
impl Solution {
pub fn longest_palindrome(s: String) -> i32 {
let mut cnt = HashMap::new();
for ch in s.chars() {
*cnt.entry(ch).or_insert(0) += 1;
}
let mut ans = 0;
for &v in cnt.values() {
ans += (v / 2) * 2;
}
if ans < (s.len() as i32) {
ans += 1;
}
ans
}
}
We can use an array or hash table
We iterate through the string
Finally, if
The time complexity is
class Solution:
def longestPalindrome(self, s: str) -> int:
odd = defaultdict(int)
cnt = 0
for c in s:
odd[c] ^= 1
cnt += 1 if odd[c] else -1
return len(s) - cnt + 1 if cnt else len(s)
class Solution {
public int longestPalindrome(String s) {
int[] odd = new int[128];
int n = s.length();
int cnt = 0;
for (int i = 0; i < n; ++i) {
odd[s.charAt(i)] ^= 1;
cnt += odd[s.charAt(i)] == 1 ? 1 : -1;
}
return cnt > 0 ? n - cnt + 1 : n;
}
}
class Solution {
public:
int longestPalindrome(string s) {
int odd[128]{};
int n = s.length();
int cnt = 0;
for (char& c : s) {
odd[c] ^= 1;
cnt += odd[c] ? 1 : -1;
}
return cnt ? n - cnt + 1 : n;
}
};
func longestPalindrome(s string) (ans int) {
odd := [128]int{}
cnt := 0
for _, c := range s {
odd[c] ^= 1
cnt += odd[c]
if odd[c] == 0 {
cnt--
}
}
if cnt > 0 {
return len(s) - cnt + 1
}
return len(s)
}
function longestPalindrome(s: string): number {
const odd: Record<string, number> = {};
let cnt = 0;
for (const c of s) {
odd[c] ^= 1;
cnt += odd[c] ? 1 : -1;
}
return cnt ? s.length - cnt + 1 : s.length;
}