forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSubstring.java
More file actions
27 lines (23 loc) · 746 Bytes
/
CountSubstring.java
File metadata and controls
27 lines (23 loc) · 746 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
package dynamic;
public class CountSubstring {
int count = 0;
public int countSubstring(String s){
if (s == null || s.length() == 0) return 0;
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++;
}
}
public static void main(String[] args) {
String s = "abc";
CountSubstring countSubstring = new CountSubstring();
int r = countSubstring.countSubstring(s);
System.out.println(r);
}
}