-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCountSubstrings.java
More file actions
64 lines (56 loc) · 1.51 KB
/
CountSubstrings.java
File metadata and controls
64 lines (56 loc) · 1.51 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.lga.algorithm.tag.homework.Week_05;
import org.junit.Test;
/**
* 647. 回文子串
* https://leetcode-cn.com/problems/palindromic-substrings/
*/
public class CountSubstrings {
/**
* 暴力解法
*
* @param s
* @return
*/
public int countSubstrings(String s) {
if (s == null || s.length() < 1) return 0;
char[] chars = s.toCharArray();
int ans = 0;
for (int i = 0; i < chars.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = i; j < chars.length; j++) {
if (isRecStr(sb.append(String.valueOf(chars[j])).toString())) ans++;
}
}
return ans;
}
private boolean isRecStr(String str) {
int end = str.length() - 1;
int start = 0;
while (start <= end) {
if (str.charAt(start++) != str.charAt(end--)) return false;
}
return true;
}
/**
* dp法
* @param s
* @return
*/
public int countSubstrings_dp(String s) {
boolean[][] dp = new boolean[s.length()][s.length()];
int ans = 0;
for (int j = 0; j < s.length(); j++) {
for (int i = 0; i <= j; i++) {
if (s.charAt(i) == s.charAt(j) && (j - i < 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
ans++;
}
}
}
return ans;
}
@Test
public void test() {
System.out.println(countSubstrings("aba"));
}
}