-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathLongestPalindrome.java
201 lines (147 loc) · 5.58 KB
/
LongestPalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Source: https://leetcode.com/problems/longest-palindrome/
Time: O(n), where n is the length of the given string(s)
Space: O(n), we need a char array, by using(s.toCharArray()), equal to the size of string(s)
NOTE: (n + m) ~ O(n), where n is the length of the given string(s)
Here, as per the constraints 1 <= s.length(n) <= 2000 and m is the number of distinct characters,
which can never be greater than 52(26 uppercase letters + 26 lowercase letters) i.e. m <= n,
so n + m <= 2n --> O(2n) --> O(n)
*/
class Solution {
public int longestPalindrome(String s) {
Map<Character, Integer> charFreq = new HashMap<>();
for(char sChar : s.toCharArray()) {
charFreq.put(sChar, charFreq.getOrDefault(sChar, 0) + 1);
}
int longestPalindrome = 0;
boolean isCharFreqOdd = false;
for(int val : charFreq.values()) {
if((val & 1) == 0) {
longestPalindrome += val;
} else {
longestPalindrome += val - 1;
if(!isCharFreqOdd) {
isCharFreqOdd = true;
longestPalindrome += 1;
}
}
}
return longestPalindrome;
}
}
------------------------------------------------------------------------------------------------------
/*
Approach 2 (Faster than Approach 1)
Time: O(n), where n is the length of the given string(s)
Space: O(n), we need a char array, by using(s.toCharArray()), equal to the size of string(s)
NOTE: (n + m) ~ O(n), where n is the length of the given string(s) and m is the size of array(charFreq) taken to
store frequency of characters present in string(s), but m will always be constant(i.e. 123) and it does not depend
on the input size, so we will treat O(n + constant_term) as O(n)
*/
class Solution {
public int longestPalindrome(String s) {
int[] charFreq = new int[123];// 123(0-122) because ASCII for small letter alphabets ends at 122(122 is ASCII of 'z')
for(char sChar : s.toCharArray()) {
++charFreq[sChar];
}
int longestPalindrome = 0;
boolean isCharFreqOdd = false;
for(int freq : charFreq) {
if(freq != 0) {
if((freq & 1) == 0) {
longestPalindrome += freq;
} else {
longestPalindrome += freq - 1;
if(!isCharFreqOdd) {
isCharFreqOdd = true;
longestPalindrome += 1;
}
}
}
}
return longestPalindrome;
}
}
------------------------------------------------------------------------------------------------------
/*
Approach 3 (Better than Approach 2)
Time: O(n), where n is the length of the given string(s)
Space: O(n), we need a char array, by using(s.toCharArray()), equal to the size of string(s)
NOTE: (n + m) ~ O(n), where n is the length of the given string(s) and m is the size of array(charFreq) taken to
store frequency of characters present in string(s), but m will always be constant(i.e. 123) and it does not depend
on the input size, so we will treat O(n + constant_term) as O(n)
*/
class Solution {
public int longestPalindrome(String s) {
int[] charFreq = new int[123];
for(char sChar : s.toCharArray()) {
++charFreq[sChar];
}
int longestPalindrome = 0;
boolean isCharFreqOdd = false;
for(int freq : charFreq) {
if(freq != 0) {
longestPalindrome += freq;
if((freq & 1) != 0) {
--longestPalindrome;
if(!isCharFreqOdd) {
isCharFreqOdd = true;
longestPalindrome += 1;
}
}
}
}
return longestPalindrome;
}
}
------------------------------------------------------------------------------------------------------
/*
Approach 4 (Better than Approach 3)
Time: O(n), where n is the length of the given string(s)
Space: O(n), we need a char array, by using(s.toCharArray()), equal to the size of string(s)
NOTE: (n + m) ~ O(n), where n is the length of the given string(s)
Here, as per the constraints 1 <= s.length(n) <= 2000 and m is the number of distinct characters,
which can never be greater than 52(26 uppercase letters + 26 lowercase letters) i.e. m <= n,
so n + m <= 2n --> O(2n) --> O(n)
*/
class Solution {
public int longestPalindrome(String s) {
Map<Character, Integer> charFreq = new HashMap<>();
for(char sChar : s.toCharArray()) {
charFreq.put(sChar, charFreq.getOrDefault(sChar, 0) + 1);
}
int charWithOddFreq = 0;
for(int val : charFreq.values()) {
if((val & 1) != 0) {
++charWithOddFreq;
}
}
int len = s.length();
return (charWithOddFreq == 0) ? len : (len - charWithOddFreq + 1);
}
}
------------------------------------------------------------------------------------------------------
/*
Approach 5(Better than approach 4)
Time: O(n), where n is the length of the given string(s)
Space: O(n), we need a char array, by using(s.toCharArray()), equal to the size of string(s)
NOTE: (n + m) ~ O(n), where n is the length of the given string(s) and m is the size of array(charFreq) taken to
store frequency of characters present in string(s), but m will always be constant(i.e. 58) and it does not depend
on the input size, so we will treat O(n + constant_term) as O(n)
*/
class Solution {
public int longestPalindrome(String s) {
int[] charFreq = new int[58]; // 58 --> 26(upper case alphabets(65 - 90)) + 26(lower case alphabets(97 - 122)) + 6(in between 'Z' and 'a')
for(char sChar : s.toCharArray()){
++charFreq[sChar - 'A'];
}
int charsWithOddFreq = 0;
for(int freq : charFreq){
if((freq & 1) != 0){
charsWithOddFreq++;
}
}
int len = s.length();
return (charsWithOddFreq == 0) ? len : (len - charsWithOddFreq + 1);
}
}