Flashcards
Palindromic Substrings - Leetcode
Cards in this deck (1)
- 1Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Input: "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".class Solution {
int count = 0;
public int countSubstrings(String s) {
for(int i = 0; i < s.length(); i++){
extendPalindrome(s,i,i);
extendPalindrome(s,i,i+1);
}
return count;
}
private void extendPalindrome(String s, int left, int right){
while(left >=0 && right < s.length() && s.charAt(left) == s.charAt(right)){
count++;
left--;
right++;
}
}
}
Information
- Description
- Flashcards on Palindromic Substrings - Leetcode, created by Suhas S on 24/03/2018.
- Created by
- Suhas S
- Views
- 2
- Rating