Flashcards
Palindromic Substrings - Leetcode
Flashcards do conjunto (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++;
}
}
}
Informações
- Descrição
- FlashCards sobre Palindromic Substrings - Leetcode, criado por Suhas S em 24-03-2018.
- Criado por
- Suhas S
- Visualizações
- 2
- Avaliação