🔒 Closed Programming exercises for beginners (Longest Palindrome in a String)

Status
Not open for further replies.

cetmyeval69

Honorary Poster
Capture3.PNG
COMMENT YOUR OWN SOLUTION! :cool::cool:



[CODE lang="java" title="My Solution"]public static String longestPalindrome(String input) {
int n = input.length();
boolean[] P = new boolean[n];
int lo = 0, hi = 0, length = 1;

for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
if (input.charAt(i) == input.charAt(j) && (i - j <= 1 || P[j + 1])) {
P[j] = true;
if (i - j + 1 > length) {
lo = j;
hi = i;
length = i - j + 1;
}
} else {
P[j] = false;
}
}
}
return input.substring(lo, hi + 1);
}[/CODE]


Implementation
Screenshot:

Capture.PNG
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 405
    Views
  • 2
    Participants
Last reply from:
PHC-Mekansm

Trending Topics

Online now

Members online
1,247
Guests online
2,307
Total visitors
3,554

Forum statistics

Threads
2,332,198
Posts
29,263,307
Members
1,146,126
Latest member
justrandomm
Back
Top