forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPalindromicSubsequenceWithONSpace.java
More file actions
33 lines (27 loc) · 1.04 KB
/
LongestPalindromicSubsequenceWithONSpace.java
File metadata and controls
33 lines (27 loc) · 1.04 KB
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
package com.geeksforgeeks.dynamicProgramming;
public class LongestPalindromicSubsequenceWithONSpace {
public static void main(String[] args) {
System.out.println(getLengthOfLPS("GEEKSFORGEEKS".toCharArray()));
}
public static int getLengthOfLPS(char[] str) {
int[] LPS = new int[str.length];
for (int i = str.length - 1; i >= 0; i--) {
int back_up = 0;
//Picking up ending points to see if str[i] increases length of LPS ending with str[j];
for (int j = i; j < str.length; j++) {
// For Length one, LPS will always be 1
if (i == j)
LPS[j] = 1;
else if (str[i] == str[j]) { // When character matches
int temp = LPS[j];
LPS[j] = back_up + 2;
back_up = temp;
} else {
back_up = LPS[j];
LPS[j] = Math.max(LPS[j - 1], LPS[j]);
}
}
}
return LPS[str.length - 1];
}
}