Skip to content

Commit

Permalink
add 242 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 10, 2024
1 parent b090efa commit e727f16
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@
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)
186| [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Python](./leetcode_python/String/reverse-words-in-a-string-ii.py) | _O(n)_ | _O(1)_ | Medium | LC 151, LC 557, 🔒, `M$`,`UBER`, `amazon` | AGAIN* (3)
242| [Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [Python](./leetcode_python/String/valid-anagram.py) | _O(n)_ | _O(1)_ | Easy | Curated Top 75, LintCode,`amazon`,`fb`|OK
242| [Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [Python](./leetcode_python/String/valid-anagram.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/ValidAnagram.java) | _O(n)_ | _O(1)_ | Easy | Curated Top 75, LintCode,`amazon`,`fb`|OK
271| [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) | [Python](./leetcode_python/String/encode-and-decode-strings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/EncodeAndDecodeStrings.java) | _O(n)_ | _O(1)_ | Medium |Curated Top 75, decode-encode,string op, check `# 394 Decode String` ,🔒, `google`| AGAIN*** (3)
306| [Addictive Number](https://leetcode.com/problems/additive-number/) | [Python](./leetcode_python/String/additive-number.py) | _O(n^3)_ | _O(n)_ | Medium | | AGAIN (not start*)
340| [Longest Substring with At Most K Distinct Characters ](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Python](./leetcode_python/String/longest-substring-with-at-most-k-distinct-characters.py) | _O(n^3)_ | _O(n)_ | Hard | string, sliding window, `google`| not start
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240310: 213,217,226,230,235
20240310: 213,217,226,230,235,238,242
20240309: 207(again!!!),208,211(again),212
20240308: 152,153(again),190,191,198,200,206(again)
20240307: 139(again),141,143(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-04 -> ['213,217,226,230,235']
2024-05-04 -> ['213,217,226,230,235,238,242']
2024-05-03 -> ['207(again!!!),208,211(again),212']
2024-05-02 -> ['152,153(again),190,191,198,200,206(again)']
2024-05-01 -> ['139(again),141,143(again)']
Expand All @@ -9,7 +9,7 @@
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-13 -> ['213,217,226,230,235']
2024-04-13 -> ['213,217,226,230,235,238,242']
2024-04-12 -> ['207(again!!!),208,211(again),212']
2024-04-11 -> ['152,153(again),190,191,198,200,206(again)']
2024-04-10 -> ['139(again),141,143(again)']
Expand All @@ -20,27 +20,27 @@
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-31 -> ['213,217,226,230,235']
2024-03-31 -> ['213,217,226,230,235,238,242']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
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-23 -> ['213,217,226,230,235']
2024-03-23 -> ['213,217,226,230,235,238,242']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217,226,230,235', '100,102,104,105(again)']
2024-03-18 -> ['213,217,226,230,235,238,242', '100,102,104,105(again)']
2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['213,217,226,230,235', '139(again),141,143(again)']
2024-03-15 -> ['213,217,226,230,235,238,242', '139(again),141,143(again)']
2024-03-14 -> ['207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['213,217,226,230,235', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217,226,230,235', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-13 -> ['213,217,226,230,235,238,242', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217,226,230,235,238,242', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
Expand Down
82 changes: 82 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/String/ValidAnagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package LeetCodeJava.String;

// https://leetcode.com/problems/valid-anagram/description/

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ValidAnagram {

// V0
// IDEA : HASHMAP
public boolean isAnagram(String s, String t) {

if (s == null && t == null){
return true;
}
// if ((s == null && t != null) || (s != null && t == null) ){
// return false;
// }
Map<String, Integer> sMap = new HashMap<>();
for (String x : s.split("")){
if (!sMap.containsKey(x)){
sMap.put(x, 1);
}
else{
sMap.put(x, sMap.get(x)+1);
}
}

for (String y : t.split("")){
if (!sMap.containsKey(y)){
return false;
}
sMap.put(y, sMap.get(y)-1);
if (sMap.get(y)==0){
sMap.remove(y);
}
}

if (sMap.keySet().size()!= 0){
return false;
}
return true;
}

// V1
// IDEA : ARRAY SORT
// https://leetcode.com/problems/valid-anagram/solutions/3687854/3-method-s-c-java-python-beginner-friendly/
public boolean isAnagram_1(String s, String t) {
char[] sChars = s.toCharArray();
char[] tChars = t.toCharArray();

Arrays.sort(sChars);
Arrays.sort(tChars);

return Arrays.equals(sChars, tChars);
}

// V2
// https://leetcode.com/problems/valid-anagram/solutions/3261552/easy-solutions-in-java-python-javascript-and-c-look-at-once/
public boolean isAnagram_2(String s, String t) {
if (s.length() != t.length()) {
return false;
}

int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
freq[t.charAt(i) - 'a']--;
}

for (int i = 0; i < freq.length; i++) {
if (freq[i] != 0) {
return false;
}
}

return true;
}

}

0 comments on commit e727f16

Please sign in to comment.