Skip to content

Commit

Permalink
add 722 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 27, 2024
1 parent b8808cc commit fa1ac01
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@
686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [Python](./leetcode_python/String/repeated-string-match.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/RepeatedStringMatch.java)| _O(n + m)_ | _O(1)_ | Medium |brute force, math, string, `Rabin-Karp Algorithm`, `google` | AGAIN**** (3)
696| [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/) | [Python](./leetcode_python/String/count-binary-substrings.py) | _O(n)_ | _O(1)_ | Easy|good trick, linear scan, group sub-string, `amazon`| AGAIN******* (3) (MUST)
720| [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](./leetcode_python/String/longest-word-in-dictionary.py) | _O(n)_ | _O(t)_ | Easy | Trie | AGAIN (not start)
722| [Remove Comments](https://leetcode.com/problems/remove-comments/) | [Python](./leetcode_python/String/remove-comments.py) | _O(n)_ | _O(k)_ | Medium || AGAIN (not start)
722| [Remove Comments](https://leetcode.com/problems/remove-comments/) | [Python](./leetcode_python/String/remove-comments.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/RemoveComments.java)| _O(n)_ | _O(k)_ | Medium |string, google| AGAIN (1)
734| [Sentence Similarity](https://leetcode.com/problems/sentence-similarity/) | [Python ](./leetcode_python/String/sentence-similarity.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/SentenceSimilarity.java) | _O(n + p)_ | _O(p)_| Easy |good basic, graph, `hash table`,`collections.defaultdict(set)` ,`google`| OK***** (5)
751| [IP to CIDR](https://leetcode.com/problems/ip-to-cidr/) |[Python](./leetcode_python/String/ip-to-cidr.py) | _O(n)_ | _O(1)_ | Medium | `ip`| AGAIN (not start*)
758| [Bold Words in String](https://leetcode.com/contest/weekly-contest-66/problems/bold-words-in-string/) | [Python](./leetcode_python/String/bold-words-in-string.py) | _O(n * l)_ | _O(t)_ | Easy | 🔒, variant of [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/) | AGAIN (not start)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-11-26
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-11-25
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

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 @@
20241126: 722
20241125: 33,81
20241124: 153
20241123: 253
Expand Down
14 changes: 9 additions & 5 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2025-01-20 -> ['722']
2025-01-19 -> ['33,81']
2025-01-17 -> ['253']
2025-01-16 -> ['776,31']
Expand All @@ -8,6 +9,7 @@
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722']
2024-12-29 -> ['33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
Expand All @@ -17,22 +19,24 @@
2024-12-21 -> ['394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722']
2024-12-16 -> ['33,81']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-09 -> ['722']
2024-12-08 -> ['33,81', '394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2024-12-05 -> ['776,31', '528,334']
2024-12-04 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-04 -> ['722', '004(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-01 -> ['253', '53,210,207']
2024-12-01 -> ['722', '253', '53,210,207']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['33,81', '776,31', '951,792', '524,221,889']
2024-11-29 -> ['722', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['722', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['722', '33,81', '776,31', '951,792', '524,221,889']
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
2024-11-25 -> ['253', '776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package LeetCodeJava.String;

import java.util.ArrayList;
import java.util.List;

// https://leetcode.com/problems/remove-comments/description/
// https://leetcode.ca/all/722.html
public class RemoveComments {

// V0
// TODO : implement
// public List<String> removeComments(String[] source) {
//
// }

// V1
// IDEA : PARSING
// https://leetcode.com/problems/remove-comments/editorial/
public List<String> removeComments_1(String[] source) {
boolean inBlock = false;
StringBuilder newline = new StringBuilder();
List<String> ans = new ArrayList();
for (String line: source) {
int i = 0;
char[] chars = line.toCharArray();
if (!inBlock) {
newline = new StringBuilder();
}
while (i < line.length()) {
if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '*') {
inBlock = true;
i++;
} else if (inBlock && i+1 < line.length() && chars[i] == '*' && chars[i+1] == '/') {
inBlock = false;
i++;
} else if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '/') {
break;
} else if (!inBlock) {
newline.append(chars[i]);
}
i++;
}
if (!inBlock && newline.length() > 0) {
ans.add(new String(newline));
}
}
return ans;
}


// V2
// https://leetcode.ca/2017-11-21-722-Remove-Comments/
public List<String> removeComments_2(String[] source) {
List<String> ans = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean blockComment = false;
for (String s : source) {
int m = s.length();
for (int i = 0; i < m; ++i) {
if (blockComment) {
if (i + 1 < m && s.charAt(i) == '*' && s.charAt(i + 1) == '/') {
blockComment = false;
++i;
}
} else {
if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '*') {
blockComment = true;
++i;
} else if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '/') {
break;
} else {
sb.append(s.charAt(i));
}
}
}
if (!blockComment && sb.length() > 0) {
ans.add(sb.toString());
sb.setLength(0);
}
}
return ans;
}

}
38 changes: 38 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,44 @@ public boolean search(int[] nums, int target) {
return false;
}

// LC 722
// https://leetcode.com/problems/remove-comments/
// 7.41 pm - 8.15 pm
/**
* Idea 1) string op
* a tmp array that collect "Not comment" element,
* then merge filtered elements as final result
*/
public List<String> removeComments(String[] source) {

if (source.length == 0){
return new ArrayList<>();
}

List<String> tmp = new ArrayList<>();
//StringBuilder sb = new StringBuilder();
boolean isMuitLineComment = false;
for (String x : source){
System.out.println(">>> x = " + x);
if (x.contains("/*") && x.contains("*/")){
tmp.add(x.split("/*")[0]);
//tmp.add(x.split("*/\")[1]);
continue;
}
if (x.contains("/*")){
isMuitLineComment = true;
}else if (x.contains("*/")){
isMuitLineComment = false;
}
if (!x.contains("//") && !isMuitLineComment){
//sb.append(x);
tmp.add(x);
}
}

return tmp;
}

}


0 comments on commit fa1ac01

Please sign in to comment.