Skip to content

Commit 0f7874c

Browse files
committed
add 833 java, progress
1 parent 77ba83f commit 0f7874c

File tree

6 files changed

+145
-14
lines changed

6 files changed

+145
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@
635635
824| [Goat Latin](https://leetcode.com/problems/goat-latin/) | [Python](./leetcode_python/String/goat-latin.py) | _O(n + w^2)_ | _O(l)_ | Easy| `string basic`, `fb`| OK
636636
828| [Count Unique Characters of All Substrings of a Given String](https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [Python](./leetcode_python/String/count-unique-characters-of-all-substrings-of-a-given-string.py) | _O(n + w^2)_ | _O(l)_ | Hard| LC 1248, dp, `string`, `amazon`| AGAIN*** (2)
637637
831| [Masking Personal Information](https://leetcode.com/problems/masking-personal-information/) | [Python](./leetcode_python/String/masking-personal-information.py) | _O(1)_ | _O(1)_ | Medium |`regular expression`| OK*
638-
833| [Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/) | [Python](./leetcode_python/String/find-and-replace-in-string.py) | _O(n + m)_ | _O(n)_ | Medium | Union Find | AGAIN (not start)
638+
833| [Find And Replace in String](https://leetcode.com/problems/find-and-replace-in-string/) | [Python](./leetcode_python/String/find-and-replace-in-string.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/FindAndReplaceInString.java) | _O(n + m)_ | _O(n)_ | Medium | hashmap, string, google| AGAIN (2) (not start)
639639
848 | [Shifting Letters](https://leetcode.com/problems/shifting-letters/) | [Python](./leetcode_python/String/shifting-letters.py) | _O(n)_ | _O(1)_ | Medium |`remainder`, `basic`| AGAIN**
640640
859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Python](./leetcode_python/String/buddy-strings.py) | _O(n)_ | _O(1)_ | Easy || AGAIN*
641641
880 | [Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/) | [Python](./leetcode_python/String/decoded-string-at-index.py) | _O(n)_ | _O(1)_ | Medium |`trick`| 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-16
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-15
47
- https://github.com/wisdompeak/LeetCode/tree/master?tab=readme-ov-file#two-pointers
58
- (2 pointer, Sliding window)

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241116: 833
12
20241114: 80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
23
20241110: 53,210,207
34
20241109: 444

data/to_review.txt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1+
2025-01-10 -> ['833']
2+
2025-01-08 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
13
2025-01-04 -> ['53,210,207']
24
2025-01-03 -> ['444']
35
2025-01-02 -> ['1188,130,855(again)']
46
2024-12-28 -> ['900']
57
2024-12-27 -> ['26,27', '802,1197,26']
68
2024-12-21 -> ['855,846']
7-
2024-12-20 -> ['932']
8-
2024-12-18 -> ['951,792']
9+
2024-12-20 -> ['833', '932']
10+
2024-12-18 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792']
911
2024-12-14 -> ['53,210,207', '163,1048']
1012
2024-12-13 -> ['444', '298,729']
1113
2024-12-12 -> ['1188,130,855(again)', '1146']
1214
2024-12-08 -> ['737']
13-
2024-12-07 -> ['900', '686,734,737']
15+
2024-12-07 -> ['833', '900', '686,734,737']
1416
2024-12-06 -> ['26,27', '802,1197,26', '353']
15-
2024-12-05 -> ['528,334']
17+
2024-12-05 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '528,334']
1618
2024-12-03 -> ['1145']
1719
2024-12-01 -> ['53,210,207']
1820
2024-11-30 -> ['444', '855,846', '1145,1219']
19-
2024-11-29 -> ['1188,130,855(again)', '932']
20-
2024-11-27 -> ['951,792', '524,221,889']
21+
2024-11-29 -> ['833', '1188,130,855(again)', '932']
22+
2024-11-27 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '951,792', '524,221,889']
2123
2024-11-26 -> ['743,889']
2224
2024-11-25 -> ['837']
23-
2024-11-24 -> ['900']
25+
2024-11-24 -> ['833', '900']
2426
2024-11-23 -> ['53,210,207', '26,27', '802,1197,26', '163,1048', '981']
25-
2024-11-22 -> ['444', '298,729', '1087']
26-
2024-11-21 -> ['1188,130,855(again)', '1146']
27+
2024-11-22 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '298,729', '1087']
28+
2024-11-21 -> ['833', '1188,130,855(again)', '1146']
2729
2024-11-20 -> ['939']
28-
2024-11-18 -> ['53,210,207', '430']
29-
2024-11-17 -> ['444', '855,846', '737', '363']
30-
2024-11-16 -> ['1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
31-
2024-11-15 -> ['53,210,207', '26,27', '802,1197,26', '353', '947']
30+
2024-11-19 -> ['833', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
31+
2024-11-18 -> ['833', '53,210,207', '430']
32+
2024-11-17 -> ['833', '80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '444', '855,846', '737', '363']
33+
2024-11-16 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
34+
2024-11-15 -> ['80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '353', '947']
3235
2024-11-14 -> ['444', '951,792', '528,334']
3336
2024-11-13 -> ['53,210,207', '1188,130,855(again)']
3437
2024-11-12 -> ['53,210,207', '444', '1145', '753']
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package LeetCodeJava.String;
2+
3+
// https://leetcode.com/problems/find-and-replace-in-string/description/
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 833. Find And Replace in String
10+
* Medium
11+
* Topics
12+
* Companies
13+
* You are given a 0-indexed string s that you must perform k replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices, sources, and targets, all of length k.
14+
* <p>
15+
* To complete the ith replacement operation:
16+
* <p>
17+
* Check if the substring sources[i] occurs at index indices[i] in the original string s.
18+
* If it does not occur, do nothing.
19+
* Otherwise if it does occur, replace that substring with targets[i].
20+
* For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "eeecd".
21+
* <p>
22+
* All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
23+
* <p>
24+
* For example, a testcase with s = "abc", indices = [0, 1], and sources = ["ab","bc"] will not be generated because the "ab" and "bc" replacements overlap.
25+
* Return the resulting string after performing all replacement operations on s.
26+
* <p>
27+
* A substring is a contiguous sequence of characters in a string.
28+
* <p>
29+
* <p>
30+
* <p>
31+
* Example 1:
32+
* <p>
33+
* <p>
34+
* Input: s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
35+
* Output: "eeebffff"
36+
* Explanation:
37+
* "a" occurs at index 0 in s, so we replace it with "eee".
38+
* "cd" occurs at index 2 in s, so we replace it with "ffff".
39+
* Example 2:
40+
* <p>
41+
* <p>
42+
* Input: s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
43+
* Output: "eeecd"
44+
* Explanation:
45+
* "ab" occurs at index 0 in s, so we replace it with "eee".
46+
* "ec" does not occur at index 2 in s, so we do nothing.
47+
* <p>
48+
* <p>
49+
* Constraints:
50+
* <p>
51+
* 1 <= s.length <= 1000
52+
* k == indices.length == sources.length == targets.length
53+
* 1 <= k <= 100
54+
* 0 <= indexes[i] < s.length
55+
* 1 <= sources[i].length, targets[i].length <= 50
56+
* s consists of only lowercase English letters.
57+
* sources[i] and targets[i] consist of only lowercase English letters.
58+
*/
59+
public class FindAndReplaceInString {
60+
61+
// V0
62+
// public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
63+
//
64+
// }
65+
66+
// V1
67+
// IDEA : HASHMAP
68+
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454170265/
69+
public String findReplaceString_1(String s, int[] indices, String[] sources, String[] targets) {
70+
Map<Integer, Integer> map = new HashMap<>();
71+
for (int i = 0; i < indices.length; i++) {
72+
if (s.startsWith(sources[i], indices[i])) {
73+
map.put(indices[i], i);
74+
}
75+
}
76+
StringBuilder sb = new StringBuilder();
77+
for (int i = 0; i < s.length(); ) {
78+
if (!map.containsKey(i)) {
79+
sb.append(s.charAt(i));
80+
i++;
81+
} else { //replace chars
82+
sb.append(targets[map.get(i)]);
83+
i += sources[map.get(i)].length();
84+
}
85+
}
86+
return sb.toString();
87+
}
88+
89+
// V2
90+
// TODO : replacer `Pair` in code
91+
// https://leetcode.com/problems/find-and-replace-in-string/submissions/1454169549/
92+
// public String findReplaceString_2(String s, int[] indices, String[] sources, String[] targets) {
93+
// Map<Integer , Pair> replacements = new TreeMap<>();
94+
// StringBuilder res = new StringBuilder();
95+
//
96+
// for(int i = 0 ; i < indices.length ; i++)
97+
// if(s.substring(indices[i]).startsWith(sources[i]))
98+
// replacements.put(indices[i] , new Pair(targets[i] , sources[i].length()-1));
99+
//
100+
// for(int i = 0 ; i < s.length() ; i++){
101+
// if(replacements.containsKey(i)){
102+
// Pair p = replacements.get(i);
103+
// res.append(p.getKey());
104+
// i += (int)p.getValue();
105+
// }
106+
// else
107+
// res.append(s.charAt(i));
108+
// }
109+
//
110+
// return res.toString();
111+
// }
112+
113+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,17 @@ public int maxSubArray(int[] nums) {
31823182
return finalSum;
31833183
}
31843184

3185+
// LC 833
3186+
// https://leetcode.com/problems/find-and-replace-in-string/
3187+
// 4.23 pm - 4.40 pm
3188+
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
3189+
3190+
// if (sources.length == 1 && targets.length == 1){
3191+
// }
3192+
3193+
return null;
3194+
}
3195+
31853196

31863197
}
31873198

0 commit comments

Comments
 (0)