Skip to content

Commit fa1ac01

Browse files
committed
add 722 java
1 parent b8808cc commit fa1ac01

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@
620620
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)
621621
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)
622622
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)
623-
722| [Remove Comments](https://leetcode.com/problems/remove-comments/) | [Python](./leetcode_python/String/remove-comments.py) | _O(n)_ | _O(k)_ | Medium || AGAIN (not start)
623+
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)
624624
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)
625625
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*)
626626
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)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-26
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-25
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241126: 722
12
20241125: 33,81
23
20241124: 153
34
20241123: 253

data/to_review.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-01-20 -> ['722']
12
2025-01-19 -> ['33,81']
23
2025-01-17 -> ['253']
34
2025-01-16 -> ['776,31']
@@ -8,6 +9,7 @@
89
2025-01-04 -> ['53,210,207']
910
2025-01-03 -> ['444']
1011
2025-01-02 -> ['1188,130,855(again)']
12+
2024-12-30 -> ['722']
1113
2024-12-29 -> ['33,81']
1214
2024-12-28 -> ['900']
1315
2024-12-27 -> ['253', '26,27', '802,1197,26']
@@ -17,22 +19,24 @@
1719
2024-12-21 -> ['394', '855,846']
1820
2024-12-20 -> ['833,950', '932']
1921
2024-12-18 -> ['951,792']
22+
2024-12-17 -> ['722']
2023
2024-12-16 -> ['33,81']
2124
2024-12-14 -> ['253', '53,210,207', '163,1048']
2225
2024-12-13 -> ['776,31', '444', '298,729']
2326
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2427
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
28+
2024-12-09 -> ['722']
2529
2024-12-08 -> ['33,81', '394', '737']
2630
2024-12-07 -> ['833,950', '900', '686,734,737']
2731
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2832
2024-12-05 -> ['776,31', '528,334']
29-
2024-12-04 -> ['004(todo),34(todo),162(todo),275(todo)']
33+
2024-12-04 -> ['722', '004(todo),34(todo),162(todo),275(todo)']
3034
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']
31-
2024-12-01 -> ['253', '53,210,207']
35+
2024-12-01 -> ['722', '253', '53,210,207']
3236
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
33-
2024-11-29 -> ['004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
34-
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)']
35-
2024-11-27 -> ['33,81', '776,31', '951,792', '524,221,889']
37+
2024-11-29 -> ['722', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
38+
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)']
39+
2024-11-27 -> ['722', '33,81', '776,31', '951,792', '524,221,889']
3640
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
3741
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']
3842
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package LeetCodeJava.String;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// https://leetcode.com/problems/remove-comments/description/
7+
// https://leetcode.ca/all/722.html
8+
public class RemoveComments {
9+
10+
// V0
11+
// TODO : implement
12+
// public List<String> removeComments(String[] source) {
13+
//
14+
// }
15+
16+
// V1
17+
// IDEA : PARSING
18+
// https://leetcode.com/problems/remove-comments/editorial/
19+
public List<String> removeComments_1(String[] source) {
20+
boolean inBlock = false;
21+
StringBuilder newline = new StringBuilder();
22+
List<String> ans = new ArrayList();
23+
for (String line: source) {
24+
int i = 0;
25+
char[] chars = line.toCharArray();
26+
if (!inBlock) {
27+
newline = new StringBuilder();
28+
}
29+
while (i < line.length()) {
30+
if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '*') {
31+
inBlock = true;
32+
i++;
33+
} else if (inBlock && i+1 < line.length() && chars[i] == '*' && chars[i+1] == '/') {
34+
inBlock = false;
35+
i++;
36+
} else if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '/') {
37+
break;
38+
} else if (!inBlock) {
39+
newline.append(chars[i]);
40+
}
41+
i++;
42+
}
43+
if (!inBlock && newline.length() > 0) {
44+
ans.add(new String(newline));
45+
}
46+
}
47+
return ans;
48+
}
49+
50+
51+
// V2
52+
// https://leetcode.ca/2017-11-21-722-Remove-Comments/
53+
public List<String> removeComments_2(String[] source) {
54+
List<String> ans = new ArrayList<>();
55+
StringBuilder sb = new StringBuilder();
56+
boolean blockComment = false;
57+
for (String s : source) {
58+
int m = s.length();
59+
for (int i = 0; i < m; ++i) {
60+
if (blockComment) {
61+
if (i + 1 < m && s.charAt(i) == '*' && s.charAt(i + 1) == '/') {
62+
blockComment = false;
63+
++i;
64+
}
65+
} else {
66+
if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '*') {
67+
blockComment = true;
68+
++i;
69+
} else if (i + 1 < m && s.charAt(i) == '/' && s.charAt(i + 1) == '/') {
70+
break;
71+
} else {
72+
sb.append(s.charAt(i));
73+
}
74+
}
75+
}
76+
if (!blockComment && sb.length() > 0) {
77+
ans.add(sb.toString());
78+
sb.setLength(0);
79+
}
80+
}
81+
return ans;
82+
}
83+
84+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,6 +3966,44 @@ public boolean search(int[] nums, int target) {
39663966
return false;
39673967
}
39683968

3969+
// LC 722
3970+
// https://leetcode.com/problems/remove-comments/
3971+
// 7.41 pm - 8.15 pm
3972+
/**
3973+
* Idea 1) string op
3974+
* a tmp array that collect "Not comment" element,
3975+
* then merge filtered elements as final result
3976+
*/
3977+
public List<String> removeComments(String[] source) {
3978+
3979+
if (source.length == 0){
3980+
return new ArrayList<>();
3981+
}
3982+
3983+
List<String> tmp = new ArrayList<>();
3984+
//StringBuilder sb = new StringBuilder();
3985+
boolean isMuitLineComment = false;
3986+
for (String x : source){
3987+
System.out.println(">>> x = " + x);
3988+
if (x.contains("/*") && x.contains("*/")){
3989+
tmp.add(x.split("/*")[0]);
3990+
//tmp.add(x.split("*/\")[1]);
3991+
continue;
3992+
}
3993+
if (x.contains("/*")){
3994+
isMuitLineComment = true;
3995+
}else if (x.contains("*/")){
3996+
isMuitLineComment = false;
3997+
}
3998+
if (!x.contains("//") && !isMuitLineComment){
3999+
//sb.append(x);
4000+
tmp.add(x);
4001+
}
4002+
}
4003+
4004+
return tmp;
4005+
}
4006+
39694007
}
39704008

39714009

0 commit comments

Comments
 (0)