|
| 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