Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Last active July 26, 2022 00:34
Show Gist options
  • Save sreeprasad/cf02802d0d21f800b845e4543cef7522 to your computer and use it in GitHub Desktop.
Save sreeprasad/cf02802d0d21f800b845e4543cef7522 to your computer and use it in GitHub Desktop.
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