From 32fa688fab07aa61f70adccf08b6f5d4d15cb5da Mon Sep 17 00:00:00 2001 From: yennanliu Date: Wed, 6 Mar 2024 18:44:52 +0800 Subject: [PATCH] add 125 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 ++--- .../LeetCodeJava/String/ValidPalindrome.java | 65 +++++++++++++++++++ 4 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java diff --git a/README.md b/README.md index 9b82e4ca..3b1e94fd 100644 --- a/README.md +++ b/README.md @@ -577,7 +577,7 @@ 058| [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [Python](./leetcode_python/String/length-of-last-word.py) | _O(n)_ | _O(1)_ | Easy || OK 067| [Add Binary](https://leetcode.com/problems/add-binary/) | [Python](./leetcode_python/String/add-binary.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/AddBinary.java) | _O(n)_ | _O(1)_ | Easy | bit op, `good basic`, `fb`, `amazon`| OK***** (7) (but again) 076| [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Python](./leetcode_python/String/minimum_window_substring.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/MinimumWindowSubstring.java) | | | Hard |`Curated Top 75` | AGAIN******* (2) (not start) -125| [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](./leetcode_python/String/valid-palindrome.py) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, LC 680, `amazon`,`fb`| OK +125| [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](./leetcode_python/String/valid-palindrome.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, LC 680, `amazon`,`fb`| OK 151| [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Python](./leetcode_python/String/reverse-words-in-a-string.py) | _O(n)_ | _O(1)_ | Medium |`amazon`,`fb`| OK (2) 161| [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) | [Python](./leetcode_python/String/one-edit-distance.py) | _O(m + n)_ | _O(1)_ | Medium |🔒, trick, recursion, string, , `fb`| AGAIN ******** (6)(AGAIN) 165| [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | [Python](./leetcode_python/String/compare-version-numbers.py) | _O(n)_ | _O(1)_ |Medium|good basic, string,`amazon`, `apple`| OK** (5) diff --git a/data/progress.txt b/data/progress.txt index 39784f57..bf0616e9 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240306: 121,124(again) +20240306: 121,124(again),125 20240305: 100,102,104,105(again) 20240304: 73,76,79(again),91,25 20240303: 55(again),56,62,70 diff --git a/data/to_review.txt b/data/to_review.txt index d7b35834..fb4c58aa 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,35 +1,35 @@ -2024-04-30 -> ['121,124(again)'] +2024-04-30 -> ['121,124(again),125'] 2024-04-29 -> ['100,102,104,105(again)'] 2024-04-28 -> ['73,76,79(again),91,25'] 2024-04-27 -> ['55(again),56,62,70'] 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-09 -> ['121,124(again)'] +2024-04-09 -> ['121,124(again),125'] 2024-04-08 -> ['100,102,104,105(again)'] 2024-04-07 -> ['73,76,79(again),91,25'] 2024-04-06 -> ['55(again),56,62,70'] 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] -2024-03-27 -> ['121,124(again)'] +2024-03-27 -> ['121,124(again),125'] 2024-03-26 -> ['100,102,104,105(again)'] 2024-03-25 -> ['73,76,79(again),91,25'] 2024-03-24 -> ['55(again),56,62,70'] 2024-03-21 -> ['39,48(again),49,53,54'] 2024-03-20 -> ['20,21,23,33(again)'] -2024-03-19 -> ['121,124(again)', '1,3,5,4,19'] +2024-03-19 -> ['121,124(again),125', '1,3,5,4,19'] 2024-03-18 -> ['100,102,104,105(again)'] 2024-03-17 -> ['73,76,79(again),91,25'] 2024-03-16 -> ['55(again),56,62,70'] -2024-03-14 -> ['121,124(again)'] +2024-03-14 -> ['121,124(again),125'] 2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54'] 2024-03-12 -> ['73,76,79(again),91,25', '20,21,23,33(again)'] -2024-03-11 -> ['121,124(again)', '55(again),56,62,70', '1,3,5,4,19'] +2024-03-11 -> ['121,124(again),125', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-10 -> ['100,102,104,105(again)'] -2024-03-09 -> ['121,124(again)', '73,76,79(again),91,25'] -2024-03-08 -> ['121,124(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] -2024-03-07 -> ['121,124(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-09 -> ['121,124(again),125', '73,76,79(again),91,25'] +2024-03-08 -> ['121,124(again),125', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-07 -> ['121,124(again),125', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54'] 2024-03-04 -> ['55(again),56,62,70', '20,21,23,33(again)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java b/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java new file mode 100644 index 00000000..cc643604 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.java @@ -0,0 +1,65 @@ +package LeetCodeJava.String; + +// https://leetcode.com/problems/valid-palindrome/description/?envType=list&envId=xoqag3yj + +/** + * A phrase is a palindrome if, after converting all uppercase letters into lowercase + * letters and removing all non-alphanumeric characters, + * it reads the same forward and backward. + * Alphanumeric characters include letters and numbers. + * + * + * NOTE !!! + * + * "and removing all non-alphanumeric characters, " + * -> so we need to consider both alphabet and numeric element + */ + +public class ValidPalindrome { + + // V0 + public boolean isPalindrome(String s) { + + if (s == null || s.length() == 0){ + return true; + } + + String sUpper = ""; + for (int idx = 0; idx < s.length(); idx++){ + if (Character.isLetterOrDigit(s.charAt(idx))){ + sUpper += String.valueOf(s.charAt(idx)).toUpperCase(); + } + } + + //System.out.println("sUpper = " + sUpper); + //String sUpper = s.toUpperCase(); + + // 2 pointer + int l = 0; + int r = sUpper.length() - 1; + while (r > l){ + if (sUpper.charAt(l) != sUpper.charAt(r)){ + return false; + } + l += 1; + r -= 1; + } + return true; + } + + // V1 + // https://leetcode.com/problems/valid-palindrome/solutions/4787889/simple-solution-using-java/?envType=list&envId=xoqag3yj + public boolean isPalindrome_1(String s) { + String str=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase(); + StringBuilder sb=new StringBuilder(str); + sb.reverse(); + String ans=sb.toString(); + for(int i=0;i