Last active
July 26, 2022 00:34
-
-
Save sreeprasad/cf02802d0d21f800b845e4543cef7522 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean[][] allSubstrings(String S) { | |
int N = S.length(); | |
int count=0; | |
boolean [][] dp = new boolean [N][N]; | |
for(int i=0;i<N;i++){ | |
dp[i][i] = true; | |
count++; | |
if(i+1<N && S.charAt(i)==S.charAt(i+1)) { | |
dp[i][i+1] = true; | |
count++; | |
} | |
} | |
for(int len=3;len<=N;len++){ | |
for(int i=0, j=i+len-1; j<N; i++, j++){ | |
if(S.charAt(i)==S.charAt(j)) { | |
dp[i][j] = dp[i+1][j-1]; | |
} | |
if(dp[i][j]) { | |
count++; | |
} | |
} | |
} | |
return dp; | |
} | |
public boolean isPalindrom(int l, int r) { | |
return dp[l][r]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment