-
Notifications
You must be signed in to change notification settings - Fork 43
/
PalindromicSubstrings.java
131 lines (117 loc) · 3.53 KB
/
PalindromicSubstrings.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package LeetCodeJava.String;
// https://leetcode.com/problems/palindromic-substrings/
public class PalindromicSubstrings {
// V0
// IDEA : 2 POINTER + for loop + odd / even length handling (gpt) (LC 005)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/String/palindromic-substrings.py#L59
public int countSubstrings(String s) {
int ans = 0;
for (int i = 0; i < s.length(); i++) {
// odd
ans += helper(s, i, i);
// even
ans += helper(s, i, i + 1);
}
return ans;
}
private int helper(String s, int l, int r) {
int ans = 0;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
l--;
r++;
ans++;
}
return ans;
}
// V0'
// IDEA : BRUTE FORCE
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/String/palindromic-substrings.py
public int countSubstrings_0(String s) {
int cnt = 0;
if (s == null || s.length() == 0){
return cnt;
}
// two pointer
// a b c
// i
// j
// i as left pointer, j as right pointer
for (int i = 0; i < s.length(); i++){
for (int j = i+1; j < s.length() + 1; j++){
if (isPalindromic(s.substring(i, j))){
cnt += 1;
}
}
}
return cnt;
}
private boolean isPalindromic(String x){
//System.out.println(x);
if (x.length() == 0){
return true;
}
if (x.length() == 1){
return true;
}
int l = 0;
int r = x.length()-1;
while (r >= l){
if (x.charAt(l) != x.charAt(r)){
return false;
}
r -= 1;
l += 1;
}
return true;
}
// V0'''
// IDEA : 2 pointers with all cases covered (gpt)
public int countSubstrings_0_1(String s) {
int n = s.length();
int res = 0;
// Expand around center for all possible centers
for (int center = 0; center < 2 * n - 1; center++) {
int left = center / 2;
int right = left + center % 2;
while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
res++;
left--;
right++;
}
}
return res;
}
// V1
// https://leetcode.com/problems/palindromic-substrings/solutions/4705208/beats-100-easy-to-understand-c-java-python-solution/
public int check(String s, int i, int j) {
int ans = 0;
while (i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)) {
ans++;
i--;
j++;
}
return ans;
}
public int countSubstrings_1(String s) {
int ans = 0;
for (int i = 0; i < s.length(); i++) {
ans += check(s, i, i);
ans += check(s, i, i + 1);
}
return ans;
}
// V2
// https://leetcode.com/problems/palindromic-substrings/solutions/4667296/short-and-easy-method-3ms-please-upvote/
public int solve(String s, int i, int j){
if(i<0 || j==s.length()) return 0;
if(s.charAt(i) == s.charAt(j)) return solve(s, i-1, j+1) + 1;
else return 0;
}
public int countSubstrings_2(String s) {
int max = 0;
for(int i=0; i<s.length(); i++){
max += solve(s,i,i) + solve(s,i,i+1);
}
return max;
}
}