forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminji-go.java
More file actions
27 lines (26 loc) · 828 Bytes
/
Copy pathminji-go.java
File metadata and controls
27 lines (26 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
Problem: https://leetcode.com/problems/palindromic-substrings/
Description: return the number of palindromic substrings in it.
Concept: Two Pointers, String, Dynamic Programming
Time Complexity: O(N²), Runtime 6ms
Space Complexity: O(1), Memory 41.62MB
*/
class Solution {
public int countSubstrings(String s) {
int totalCount = 0;
for(int i=0; i<s.length(); i++){
totalCount+= countPalindromes(s, i, i);
totalCount+= countPalindromes(s, i, i+1);
}
return totalCount;
}
public int countPalindromes(String s, int left, int right){
int count = 0;
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)) {
count++;
right++;
left--;
}
return count;
}
}