Skip to content

Commit f625aac

Browse files
committed
feat: palindromic substrings
1 parent d6d11fe commit f625aac

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Problem: https://leetcode.com/problems/palindromic-substrings/
3+
Description: return the number of palindromic substrings in it.
4+
Concept: Two Pointers, String, Dynamic Programming
5+
Time Complexity: O(N²), Runtime 6ms
6+
Space Complexity: O(1), Memory 41.62MB
7+
*/
8+
class Solution {
9+
public int countSubstrings(String s) {
10+
int totalCount = 0;
11+
for(int i=0; i<s.length(); i++){
12+
totalCount+= countPalindromes(s, i, i);
13+
totalCount+= countPalindromes(s, i, i+1);
14+
}
15+
return totalCount;
16+
}
17+
18+
public int countPalindromes(String s, int left, int right){
19+
int count = 0;
20+
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)) {
21+
count++;
22+
right++;
23+
left--;
24+
}
25+
return count;
26+
}
27+
}

0 commit comments

Comments
 (0)