https://leetcode.com/problems/palindromic-substrings/
- String
- Two Pointer
- DP
Count palindromes by expanding around each center (odd and even length).
O(n^2)
O(1)
class Solution {
private int count = 0;
private void expandAroundCenter(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
count++;
left--;
right++;
}
}
public int countSubstrings(String s) {
for (int i = 0; i < s.length(); i++) {
expandAroundCenter(s, i, i);
expandAroundCenter(s, i, i + 1);
}
return count;
}
}