forked from ghostmkg/programming-language
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhave.java
More file actions
Latest commit
36 lines (30 loc) · 862 Bytes
/
Copy pathhave.java
File metadata and controls
36 lines (30 loc) · 862 Bytes
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
publicclassSolution {
publicStringlongestPalindrome(Strings) {
if (s.length() <= 1) {
returns;
}
intmaxLen = 1;
StringmaxStr = s.substring(0, 1);
for (inti = 0; i < s.length(); i++) {
for (intj = i + maxLen; j <= s.length(); j++) {
if (j - i > maxLen && isPalindrome(s.substring(i, j))) {
maxLen = j - i;
maxStr = s.substring(i, j);
}
}
}
returnmaxStr;
}
privatebooleanisPalindrome(Stringstr) {
intleft = 0;
intright = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
returnfalse;
}
left++;
right--;
}
returntrue;
}
}