-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
leetcode_java/src/main/java/LeetCodeJava/String/PalindromicSubstrings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package LeetCodeJava.String; | ||
|
||
// https://leetcode.com/problems/palindromic-substrings/ | ||
|
||
public class PalindromicSubstrings { | ||
|
||
// V0 | ||
// IDEA : BRUTE FORCE | ||
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/String/palindromic-substrings.py | ||
public int countSubstrings(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; | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters