Skip to content

Commit

Permalink
add 647 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 14, 2024
1 parent 049fd96 commit 3374619
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@
556| [Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/) | [Python](./leetcode_python/String/next-greater-element-iii.py) | _O(1)_ | _O(1)_ | Medium | | AGAIN (not start)
557| [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](./leetcode_python/String/reverse-words-in-a-string-iii.py) | _O(n)_ | _O(1)_ | Easy | | OK
616| [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/) | [Python](./leetcode_python/String/add-bold-tag-in-string.py) | _O(n * d * l)_ | _O(n)_ | Medium | 🔒, `google` | AGAIN (not start)
647| [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Python](./leetcode_python/String/palindromic-substrings.py) |_O(n)_ | _O(n)_ | Medium | Curated Top 75, LC 005, greedy, DP, expand from center, 2 pointers, `good basic`,`Manacher's Algorithm`, `Linkedin`,`amazon`,`fb` | OK******* (7) (MUST)
647| [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Python](./leetcode_python/String/palindromic-substrings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/PalindromicSubstrings.java) |_O(n)_ | _O(n)_ | Medium | Curated Top 75, LC 005, greedy, DP, expand from center, 2 pointers, `good basic`,`Manacher's Algorithm`, `Linkedin`,`amazon`,`fb` | OK******* (7) (MUST)
648| [Replace Words](https://leetcode.com/problems/replace-words/) | [Python](./leetcode_python/String/replace-words.py) | _O(n)_ | _O(t)_ | Medium | Trie, `good basic`| OK*
657| [Judge Route Circle](https://leetcode.com/articles/judge-route-circle/) | [Python](./leetcode_python/String/judge-route-circle.py) | _O(n)_ | _O(1)_ | Easy | | OK
678| [Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/) | [Python](./leetcode_python/String/valid-palindrome.py) | _O(n)_ | _O(1)_ | Medium | | AGAIN**
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240314: 647
20240313: 338,347,371,417(again),424(again),435,572(again)
20240312: 297(again),300,322(again),323
20240311: 261(again!!!),268,269(again),271,295(again)
Expand Down
16 changes: 8 additions & 8 deletions data/to_review.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@
2022-03-13 -> ['815,297', '669,1353,143,330,2021,207,417', '053,091,353,348', '1109,545,2021,040', '1567,012,013,863,926,370', '642,460,1481', '828,937,1268,926,696', '026,080,257,11,128', '189,617']
2022-03-12 -> ['669,1353,143,330,2021,207,417', '053,091,353,348', '694,1915', '207,1135,1703', '1041,1339,140,133,139', '239,1375,089,190,735,696', '734,737,743,787,130', '69,207,210', '560,264,703']
2022-03-11 -> ['053,091,353,348', '694,1915', '1109,545,2021,040', '1248,828,139,1372', '1010,767,079,582', '588,1730,042', '686,298,127,126,133', '1010,134,089']
2022-03-10 -> ['694,1915', '1109,545,2021,040', '207,1135,1703', '1567,012,013,863,926,370', '139,140', '1151,2104,212,1648', '308,269,206,079', '111,165,57,56,355', '647']
2022-03-10 -> ['647', '694,1915', '1109,545,2021,040', '207,1135,1703', '1567,012,013,863,926,370', '139,140', '1151,2104,212,1648', '308,269,206,079', '111,165,57,56,355']
2022-03-09 -> ['1109,545,2021,040', '207,1135,1703', '1248,828,139,1372', '1041,1339,140,133,139', '907,252,253,019', '681,340,413,482', '323,008,538']
2022-03-08 -> ['207,1135,1703', '1248,828,139,1372', '1567,012,013,863,926,370', '1010,767,079,582', '642,460,1481', '1710,472', '388,683', '716,136,163,725,621']
2022-03-07 -> ['1248,828,139,1372', '1567,012,013,863,926,370', '1041,1339,140,133,139', '139,140', '239,1375,089,190,735,696', '370,393,113', '57,56,227,224']
Expand All @@ -514,7 +514,7 @@
2022-02-20 -> ['734,737,743,787,130', '686,298,127,126,133', '308,269,206,079', '388,683', '026,080,257,11,128', '235,236,1268', '189,617']
2022-02-19 -> ['686,298,127,126,133', '308,269,206,079', '681,340,413,482', '69,207,210', '560,264,703']
2022-02-18 -> ['308,269,206,079', '681,340,413,482', '388,683', '565', '1167', '1010,134,089']
2022-02-17 -> ['681,340,413,482', '388,683', '111,165,57,56,355', '647']
2022-02-17 -> ['647', '681,340,413,482', '388,683', '111,165,57,56,355']
2022-02-16 -> ['388,683', '670,406,1010,525,523', '406', '323,008,538']
2022-02-15 -> ['565', '026,080,257,11,128', '005,026,080,011', '716,136,163,725,621']
2022-02-14 -> ['69,207,210', '791', '57,56,227,224']
Expand All @@ -527,26 +527,26 @@
2022-02-07 -> ['69,207,210', '111,165,57,56,355', '235,236,1268', '005,026,080,011', '189,617']
2022-02-06 -> ['111,165,57,56,355', '791', '560,264,703']
2022-02-05 -> ['111,165,57,56,355', '190', '1167', '640', '1010,134,089', '937,1041,994,523,525,204']
2022-02-04 -> ['235,236,1268', '553,606,662,295', '647', '090,852,1131']
2022-02-04 -> ['647', '235,236,1268', '553,606,662,295', '090,852,1131']
2022-02-03 -> ['190', '406', '536,532', '323,008,538']
2022-02-02 -> ['190', '235,236,1268', '1167', '005,026,080,011', '532', '716,136,163,725,621']
2022-02-01 -> ['190', '235,236,1268', '791', '100,572,508', '57,56,227,224']
2022-01-31 -> ['235,236,1268', '1167', '406', '640', '046,969,128']
2022-01-30 -> ['1167', '005,026,080,011', '553,606,662,295', '189,617', '658,005']
2022-01-29 -> ['1167', '406', '791', '536,532', '560,264,703', '875,646,435,055']
2022-01-28 -> ['406', '005,026,080,011', '640', '532', '1010,134,089', '508,663,725,278,035,875']
2022-01-27 -> ['406', '005,026,080,011', '791', '553,606,662,295', '100,572,508', '647', '529,508']
2022-01-27 -> ['647', '406', '005,026,080,011', '791', '553,606,662,295', '100,572,508', '529,508']
2022-01-26 -> ['005,026,080,011', '791', '640', '536,532', '323,008,538', '073,199']
2022-01-25 -> ['791', '640', '553,606,662,295', '532', '189,617', '716,136,163,725,621']
2022-01-24 -> ['640', '553,606,662,295', '536,532', '100,572,508', '560,264,703', '57,56,227,224']
2022-01-23 -> ['553,606,662,295', '536,532', '532', '1010,134,089', '046,969,128', '937,1041,994,523,525,204']
2022-01-22 -> ['536,532', '532', '100,572,508', '189,617', '647', '658,005', '090,852,1131']
2022-01-22 -> ['647', '536,532', '532', '100,572,508', '189,617', '658,005', '090,852,1131']
2022-01-21 -> ['532', '100,572,508', '560,264,703', '323,008,538', '875,646,435,055']
2022-01-20 -> ['100,572,508', '189,617', '1010,134,089', '716,136,163,725,621', '508,663,725,278,035,875']
2022-01-19 -> ['189,617', '560,264,703', '647', '57,56,227,224', '529,508']
2022-01-19 -> ['647', '189,617', '560,264,703', '57,56,227,224', '529,508']
2022-01-18 -> ['189,617', '560,264,703', '1010,134,089', '323,008,538', '046,969,128', '073,199']
2022-01-17 -> ['560,264,703', '1010,134,089', '647', '716,136,163,725,621', '658,005']
2022-01-16 -> ['1010,134,089', '647', '323,008,538', '57,56,227,224', '875,646,435,055']
2022-01-17 -> ['647', '560,264,703', '1010,134,089', '716,136,163,725,621', '658,005']
2022-01-16 -> ['647', '1010,134,089', '323,008,538', '57,56,227,224', '875,646,435,055']
2022-01-15 -> ['647', '323,008,538', '716,136,163,725,621', '046,969,128', '508,663,725,278,035,875', '937,1041,994,523,525,204']
2022-01-14 -> ['323,008,538', '716,136,163,725,621', '57,56,227,224', '658,005', '529,508', '090,852,1131']
2022-01-13 -> ['716,136,163,725,621', '57,56,227,224', '046,969,128', '875,646,435,055', '073,199']
Expand Down
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;
}

}
22 changes: 18 additions & 4 deletions leetcode_java/src/main/java/dev/Test2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

public class Test2 {
public static void main(String[] args) {
String word = "heloooo 123 111";
for (String x : word.split("")){
System.out.println(x);
}

// String word = "heloooo 123 111";
// for (String x : word.split("")){
// System.out.println(x);
// }

int r = 3;
System.out.println("start");
while (r >= 0){
r -= 1;
System.out.println(r);
if (r == 2){
break;
//continue;
}
}
System.out.println("end");

}

}

0 comments on commit 3374619

Please sign in to comment.