Skip to content

Commit 69e9b9a

Browse files
committed
leetcode
1 parent b958ab1 commit 69e9b9a

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
3+
-* 409. Longest Palindrome *-
4+
5+
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.
6+
7+
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
8+
9+
10+
11+
Example 1:
12+
13+
Input: s = "abccccdd"
14+
Output: 7
15+
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
16+
Example 2:
17+
18+
Input: s = "a"
19+
Output: 1
20+
Explanation: The longest palindrome that can be built is "a", whose length is 1.
21+
22+
23+
Constraints:
24+
25+
1 <= s.length <= 2000
26+
s consists of lowercase and/or uppercase English letters only.
27+
28+
*/
29+
30+
class Solution {
31+
int longestPalindrome(String s) {
32+
Map<String, int> frequency = {};
33+
int length = 0;
34+
35+
// Count the frequency of each letter
36+
for (int i = 0; i < s.length; i++) {
37+
String char = s[i];
38+
frequency[char] = (frequency[char] ?? 0) + 1;
39+
}
40+
41+
// Iterate through the frequency counts
42+
frequency.values.forEach((count) {
43+
if (count % 2 == 0) {
44+
length += count;
45+
} else {
46+
length += count - 1;
47+
}
48+
});
49+
50+
// Check for remaining letters with odd counts
51+
if (length < s.length) {
52+
length += 1;
53+
}
54+
55+
return length;
56+
}
57+
}
58+
59+
class B {
60+
int longestPalindrome(String s) {
61+
List<int> count = List<int>.filled(256, 0);
62+
int odds = 0;
63+
64+
for (int i = 0; i < s.length; i++) {
65+
int char = s.codeUnitAt(i);
66+
count[char]++;
67+
if (count[char] % 2 == 1) {
68+
odds++;
69+
} else {
70+
odds--;
71+
}
72+
}
73+
74+
return s.length - odds + boolToInt(odds > 0);
75+
}
76+
77+
int boolToInt(bool b) {
78+
return b ? 1 : 0;
79+
}
80+
}
81+
82+
class C {
83+
int longestPalindrome(String s) {
84+
int dp = s.runes.fold(0, (int acc, int x) {
85+
acc ^= 1 << (x - 65);
86+
return acc;
87+
});
88+
89+
return s.length - countSetBits(dp) + (dp != 0 ? 1 : 0);
90+
}
91+
92+
int countSetBits(int n) {
93+
int count = 0;
94+
while (n != 0) {
95+
count += n & 1;
96+
n >>= 1;
97+
}
98+
return count;
99+
}
100+
}
101+
102+
class BitSet {
103+
int _value;
104+
105+
BitSet() : _value = 0;
106+
107+
void flip(int k) {
108+
_value ^= (1 << k);
109+
}
110+
111+
bool get(int k) {
112+
return ((_value >> k) & 1) == 1;
113+
}
114+
}
115+
116+
class E {
117+
int longestPalindrome(String s) {
118+
if (s.isEmpty) {
119+
return 0;
120+
}
121+
122+
BitSet bitSet = BitSet();
123+
int ans = 0;
124+
125+
for (int ch in s.runes) {
126+
int pos = 0;
127+
128+
if (ch >= 65 && ch <= 90) {
129+
pos = ch - 65 + 26;
130+
} else {
131+
pos = ch - 97;
132+
}
133+
134+
if (bitSet.get(pos)) {
135+
ans += 2;
136+
}
137+
138+
bitSet.flip(pos);
139+
}
140+
141+
return min(ans + 1, s.length);
142+
}
143+
144+
int min(int a, int b) {
145+
return a < b ? a : b;
146+
}
147+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
func longestPalindrome1(s string) int {
4+
frequency := make(map[string]int)
5+
length := 0
6+
7+
// Count the frequency of each letter
8+
for i := 0; i < len(s); i++ {
9+
char := string(s[i])
10+
frequency[char] = frequency[char] + 1
11+
}
12+
13+
// Iterate through the frequency counts
14+
for _, count := range frequency {
15+
if count%2 == 0 {
16+
length += count
17+
} else {
18+
length += count - 1
19+
}
20+
}
21+
22+
// Check for remaining letters with odd counts
23+
if length < len(s) {
24+
length += 1
25+
}
26+
27+
return length
28+
}
29+
30+
// type Solution struct{}
31+
32+
func longestPalindrome(str string) int {
33+
count := make([]int, 256)
34+
odds := 0
35+
36+
for _, char := range str {
37+
count[char]++
38+
if count[char]&1 == 1 {
39+
odds++
40+
} else {
41+
odds--
42+
}
43+
}
44+
45+
return len(str) - odds + boolToInt(odds > 0)
46+
}
47+
48+
func boolToInt(b bool) int {
49+
if b {
50+
return 1
51+
}
52+
return 0
53+
}

LongestPalindrome/longest_palindrome.md

Whitespace-only changes.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
223223
- [**1232.** Check If It Is a Straight Line](CheckIfItIsAStraightLine/check_if_it_is_a_straight_line.dart)
224224
- [**405.** Convert a Number to Hexadecimal](ConvertANumberToHexadecimal/convert_a_number_to_hexadecimal.dart)
225225
- [**1502.** Can Make Arithmetic Progression From Sequence](CanMakeArithmeticProgressionFromSequence/can_make_arithmetic_progression_from_sequence.dart)
226+
- [**409.** Longest Palindrome](LongestPalindrome/longest_palindrome.dart)
226227

227228
## Reach me via
228229

0 commit comments

Comments
 (0)