-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSubstrings.java
More file actions
44 lines (41 loc) · 1.18 KB
/
countSubstrings.java
File metadata and controls
44 lines (41 loc) · 1.18 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Author : WindAsMe
* File : countSubstrings.java
* Time : Create on 18-8-8
* Location : ../Home/JavaForLeeCode2/countSubstrings.java
* Function : LeetCode No.647
*/
public class countSubstrings {
private static int countSubstringsResult(String s) {
int ans = s.length();
if (s.length() < 2)
return ans;
// the stride
for (int i = 2; i <= s.length(); i++) {
// j: start j + i: end
for (int j = 0; j + i <= s.length(); j++) {
// s.substring(): This function
// include the start value
// exclude the end value
if (valid(s.substring(j, j + i)))
ans++;
}
}
return ans;
}
private static boolean valid(String s) {
char[] c = s.toCharArray();
int start = 0;
int end = c.length - 1;
while (start < end) {
if (c[start] != c[end])
return false;
start++;
end--;
}
return true;
}
public static void main(String[] args) {
System.out.println(countSubstringsResult("aaa"));
}
}