Skip to content

Commit 212b1a0

Browse files
committed
add 792 java
1 parent 809eb84 commit 212b1a0

File tree

5 files changed

+364
-11
lines changed

5 files changed

+364
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
755 | [Pour Water](https://leetcode.com/problems/pour-water/) | [Python](./leetcode_python/Array/pour-water.py) | _O(v * n)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)
276276
766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Python](./leetcode_python/Array/toeplitz-matrix.py) | _O(m * n)_ | _O(1)_ | Easy |`basic`, `matrix`, `google`| AGAIN* (2)
277277
769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | [Python](./leetcode_python/Array/max-chunks-to-make-sorted.py) | _O(n)_ | _O(1)_ | Medium || AGAIN** (not start)
278-
792 | [Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/) | [Python](./leetcode_python/Array/number-of-matching-subsequences.py) | _O(n + w)_ | _O(1)_ | Medium |`trick`| AGAIN* (not start)
278+
792 | [Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/) | [Python](./leetcode_python/Array/number-of-matching-subsequences.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/NumberOfMatchingSubsequences.java) | _O(n + w)_ | _O(1)_ | Medium |basic, hash map, google| AGAIN**** (2)
279279
794 | [Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/) | [Python](./leetcode_python/Array/valid-tic-tac-toe-state.py) | _O(1)_ | _O(1)_ | Medium |`complex`| AGAIN
280280
795 | [Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/) | [Python](./leetcode_python/Array/number-of-subarrays-with-bounded-maximum.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start*)
281281
807 | [Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/) | [Python](./leetcode_python/Array/max-increase-to-keep-city-skyline.py) | _O(n^2)_ | _O(n)_ | Medium || AGAIN*

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241024: 951
1+
20241024: 951,792
22
20241020: 163,1048
33
20241019: 298,729
44
20241018: 1146

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2024-12-18 -> ['951']
1+
2024-12-18 -> ['951,792']
22
2024-12-14 -> ['163,1048']
33
2024-12-13 -> ['298,729']
44
2024-12-12 -> ['1146']
@@ -8,7 +8,7 @@
88
2024-12-05 -> ['528,334']
99
2024-12-03 -> ['1145']
1010
2024-11-30 -> ['1145,1219']
11-
2024-11-27 -> ['951', '524,221,889']
11+
2024-11-27 -> ['951,792', '524,221,889']
1212
2024-11-26 -> ['743,889']
1313
2024-11-25 -> ['837']
1414
2024-11-23 -> ['163,1048', '981']
@@ -19,25 +19,25 @@
1919
2024-11-17 -> ['737', '363']
2020
2024-11-16 -> ['686,734,737', '1032,844,1011']
2121
2024-11-15 -> ['353', '947']
22-
2024-11-14 -> ['951', '528,334']
22+
2024-11-14 -> ['951,792', '528,334']
2323
2024-11-12 -> ['1145', '753']
2424
2024-11-11 -> ['727']
2525
2024-11-10 -> ['163,1048']
2626
2024-11-09 -> ['298,729', '1145,1219']
2727
2024-11-08 -> ['1146']
28-
2024-11-06 -> ['951', '524,221,889']
28+
2024-11-06 -> ['951,792', '524,221,889']
2929
2024-11-05 -> ['743,889']
3030
2024-11-04 -> ['737', '837', '659']
3131
2024-11-03 -> ['686,734,737', '801,552']
3232
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
33-
2024-11-01 -> ['951', '298,729', '528,334', '1087']
33+
2024-11-01 -> ['951,792', '298,729', '528,334', '1087']
3434
2024-10-31 -> ['1146']
3535
2024-10-30 -> ['1145', '939']
36-
2024-10-29 -> ['951']
36+
2024-10-29 -> ['951,792']
3737
2024-10-28 -> ['163,1048', '430']
38-
2024-10-27 -> ['951', '298,729', '737', '1145,1219', '363']
39-
2024-10-26 -> ['951', '1146', '686,734,737', '1032,844,1011']
40-
2024-10-25 -> ['951', '163,1048', '353', '947', '1110, 1055']
38+
2024-10-27 -> ['951,792', '298,729', '737', '1145,1219', '363']
39+
2024-10-26 -> ['951,792', '1146', '686,734,737', '1032,844,1011']
40+
2024-10-25 -> ['951,792', '163,1048', '353', '947', '1110, 1055']
4141
2024-10-24 -> ['298,729', '528,334', '524,221,889']
4242
2024-10-23 -> ['163,1048', '1146', '743,889']
4343
2024-10-22 -> ['163,1048', '298,729', '737', '1145', '837', '753']
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/number-of-matching-subsequences/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
* 792. Number of Matching Subsequences
9+
* Medium
10+
* Topics
11+
* Companies
12+
* Given a string s and an array of strings words, return the number of words[i] that is a subsequence of s.
13+
* <p>
14+
* A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
15+
* <p>
16+
* For example, "ace" is a subsequence of "abcde".
17+
* <p>
18+
* <p>
19+
* Example 1:
20+
* <p>
21+
* Input: s = "abcde", words = ["a","bb","acd","ace"]
22+
* Output: 3
23+
* Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
24+
* Example 2:
25+
* <p>
26+
* Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
27+
* Output: 2
28+
* <p>
29+
* <p>
30+
* Constraints:
31+
* <p>
32+
* 1 <= s.length <= 5 * 104
33+
* 1 <= words.length <= 5000
34+
* 1 <= words[i].length <= 50
35+
* and words[i] consist of only lowercase English letters.
36+
*/
37+
public class NumberOfMatchingSubsequences {
38+
39+
// V0
40+
// public int numMatchingSubseq(String s, String[] words) {
41+
//
42+
// }
43+
44+
// V1
45+
// IDEA : HASH MAP + 2 POINTERS
46+
// https://leetcode.com/problems/number-of-matching-subsequences/solutions/2306416/java-easy-solution-97-faster-code/
47+
public int numMatchingSubseq_1(String s, String[] words) {
48+
49+
Map<String, Integer> map = new HashMap<>();
50+
for (String str : words) {
51+
map.put(str, map.getOrDefault(str, 0) + 1);
52+
}
53+
54+
int ans = 0;
55+
char ch[] = s.toCharArray();
56+
57+
for (String str : map.keySet()) {
58+
59+
char temp[] = str.toCharArray();
60+
int i = 0;
61+
int j = 0;
62+
63+
while (i < ch.length && j < temp.length) {
64+
if (ch[i] == temp[j]) {
65+
i++;
66+
j++;
67+
} else {
68+
i++;
69+
}
70+
}
71+
72+
if (j == temp.length) {
73+
ans += map.get(str);
74+
}
75+
76+
}
77+
78+
return ans;
79+
}
80+
81+
// V2
82+
// https://leetcode.com/problems/number-of-matching-subsequences/solutions/1097868/simple-java-solution-used-a-map-for-last-7-test-cases/
83+
public static int numMatchingSubseq_2(String S, String[] words) {
84+
int result = 0;
85+
86+
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
87+
for (String word : words) {
88+
if (!map.containsKey(word)) {
89+
if (isSubSequence(word, S, word.length(), S.length()) || word.length() == 0) {
90+
result++;
91+
map.put(word, true);
92+
} else {
93+
map.put(word, false);
94+
}
95+
} else {
96+
if (map.get(word)) {
97+
result++;
98+
}
99+
}
100+
101+
}
102+
103+
return result;
104+
}
105+
106+
static boolean isSubSequence(String str1, String str2, int m, int n) {
107+
int j = 0;
108+
109+
// Traverse str2 and str1, and compare
110+
// current character of str2 with first
111+
// unmatched char of str1, if matched
112+
// then move ahead in str1
113+
for (int i = 0; i < n && j < m; i++)
114+
if (str1.charAt(j) == str2.charAt(i))
115+
j++;
116+
117+
// If all characters of str1 were found
118+
// in str2
119+
return (j == m);
120+
}
121+
122+
// V3
123+
// https://leetcode.com/problems/number-of-matching-subsequences/solutions/2306285/java-easy-solution-using-hashmap/
124+
public int numMatchingSubseq_3(String s, String[] words) {
125+
Map<Character, Queue<String>> mp = new HashMap<>();
126+
int ans = 0;
127+
128+
for (int i = 0; i < s.length(); ++i)
129+
mp.putIfAbsent(s.charAt(i), new LinkedList<>());
130+
131+
for (String word : words) {
132+
char startCh = word.charAt(0);
133+
if (mp.containsKey(startCh))
134+
mp.get(startCh).offer(word);
135+
}
136+
137+
for (int i = 0; i < s.length(); ++i) {
138+
char startCh = s.charAt(i);
139+
Queue<String> que = mp.get(startCh);
140+
int size = que.size();
141+
for (int j = 0; j < size; ++j) {
142+
String str = que.poll();
143+
if (str.substring(1).length() == 0)
144+
ans++;
145+
else if (mp.containsKey(str.charAt(1)))
146+
mp.get(str.charAt(1)).add(str.substring(1));
147+
}
148+
}
149+
150+
return ans;
151+
}
152+
153+
// V4
154+
// https://leetcode.com/problems/number-of-matching-subsequences/solutions/1289458/easy-to-understand-java-solution-explanation-with-comments-with-string-functions/
155+
/* Approach: For every word, check if it is subsequence of input string */
156+
public int numMatchingSubseq_4(String s, String[] words) {
157+
158+
String inputString = s;
159+
int count = 0;
160+
161+
// Check for every words in array
162+
163+
for (String word : words) {
164+
165+
// Check if word is subsequence of input string
166+
167+
if (checkSubsequence(word, inputString)) {
168+
count = count + 1;
169+
}
170+
171+
}
172+
173+
return count;
174+
}
175+
176+
177+
/* Helper function to check if given word is sub sequence of given input string */
178+
179+
private boolean checkSubsequence(String word, String inputString) {
180+
181+
int prevCharIndex = 0; // It will store the index of input String where previous char was found
182+
183+
/* So, the curr character should be found after this index for maintaining subsequence order */
184+
185+
for (char ch : word.toCharArray()) {
186+
187+
int index = inputString.indexOf(ch, prevCharIndex); // search for char after prev char found index
188+
189+
// If index == -1 means char not found, else found at index i.
190+
191+
if (index == -1) {
192+
return false;
193+
}
194+
195+
prevCharIndex = index + 1; // set the prevCharIndex to current found char index + 1 for next search
196+
197+
// We do index + 1 as maybe duplicate elements consider this same index twice, so increment by 1.
198+
199+
}
200+
201+
return true; // Every chars traversed and found, return true.
202+
203+
}
204+
205+
206+
// V5
207+
// IDEA : list + queue (gpt)
208+
public int numMatchingSubseq_5(String s, String[] words) {
209+
// Create an array of 26 queues (one for each letter)
210+
List<Queue<String>> waiting = new ArrayList<>(26);
211+
for (int i = 0; i < 26; i++) {
212+
waiting.add(new LinkedList<>());
213+
}
214+
215+
// Add each word to the appropriate queue based on its first letter
216+
for (String word : words) {
217+
char firstChar = word.charAt(0);
218+
waiting.get(firstChar - 'a').offer(word);
219+
}
220+
221+
// Process the string `s`
222+
int res = 0;
223+
for (char c : s.toCharArray()) {
224+
Queue<String> queue = waiting.get(c - 'a');
225+
int size = queue.size(); // Only process the current size of the queue
226+
while (size-- > 0) {
227+
String word = queue.poll();
228+
if (word.length() == 1) {
229+
// If the word has only one letter and it's matched, it's a subsequence
230+
res++;
231+
} else {
232+
// Move the remaining part of the word to the next appropriate queue
233+
String remaining = word.substring(1);
234+
waiting.get(remaining.charAt(0) - 'a').offer(remaining);
235+
}
236+
}
237+
}
238+
239+
return res;
240+
}
241+
242+
// V2
243+
}

0 commit comments

Comments
 (0)