File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments