|
1 | | -1031\. Maximum Sum of Two Non-Overlapping Subarrays |
| 1 | +1032\. Stream of Characters |
2 | 2 |
|
3 | | -Medium |
| 3 | +Hard |
4 | 4 |
|
5 | | -Given an integer array `nums` and two integers `firstLen` and `secondLen`, return _the maximum sum of elements in two non-overlapping **subarrays** with lengths_ `firstLen` _and_ `secondLen`. |
| 5 | +Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings `words`. |
6 | 6 |
|
7 | | -The array with length `firstLen` could occur before or after the array with length `secondLen`, but they have to be non-overlapping. |
| 7 | +For example, if `words = ["abc", "xyz"]` and the stream added the four characters (one by one) `'a'`, `'x'`, `'y'`, and `'z'`, your algorithm should detect that the suffix `"xyz"` of the characters `"axyz"` matches `"xyz"` from `words`. |
8 | 8 |
|
9 | | -A **subarray** is a **contiguous** part of an array. |
| 9 | +Implement the `StreamChecker` class: |
10 | 10 |
|
11 | | -**Example 1:** |
12 | | - |
13 | | -**Input:** nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2 |
14 | | - |
15 | | -**Output:** 20 |
16 | | - |
17 | | -**Explanation:** One choice of subarrays is [9] with length 1, and [6,5] with length 2. |
| 11 | +* `StreamChecker(String[] words)` Initializes the object with the strings array `words`. |
| 12 | +* `boolean query(char letter)` Accepts a new character from the stream and returns `true` if any non-empty suffix from the stream forms a word that is in `words`. |
18 | 13 |
|
19 | | -**Example 2:** |
20 | | - |
21 | | -**Input:** nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2 |
22 | | - |
23 | | -**Output:** 29 |
24 | | - |
25 | | -**Explanation:** One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2. |
| 14 | +**Example 1:** |
26 | 15 |
|
27 | | -**Example 3:** |
| 16 | +**Input** ["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"] [[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]] |
28 | 17 |
|
29 | | -**Input:** nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3 |
| 18 | +**Output:** [null, false, false, false, true, false, true, false, false, false, false, false, true] |
30 | 19 |
|
31 | | -**Output:** 31 |
| 20 | +**Explanation:** |
32 | 21 |
|
33 | | -**Explanation:** One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3. |
| 22 | + StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]); |
| 23 | + streamChecker.query("a"); // return False |
| 24 | + streamChecker.query("b"); // return False |
| 25 | + streamChecker.query("c"); // return False |
| 26 | + streamChecker.query("d"); // return True, because 'cd' is in the wordlist |
| 27 | + streamChecker.query("e"); // return False |
| 28 | + streamChecker.query("f"); // return True, because 'f' is in the wordlist |
| 29 | + streamChecker.query("g"); // return False |
| 30 | + streamChecker.query("h"); // return False |
| 31 | + streamChecker.query("i"); // return False |
| 32 | + streamChecker.query("j"); // return False |
| 33 | + streamChecker.query("k"); // return False |
| 34 | + streamChecker.query("l"); // return True, because 'kl' is in the wordlist |
34 | 35 |
|
35 | 36 | **Constraints:** |
36 | 37 |
|
37 | | -* `1 <= firstLen, secondLen <= 1000` |
38 | | -* `2 <= firstLen + secondLen <= 1000` |
39 | | -* `firstLen + secondLen <= nums.length <= 1000` |
40 | | -* `0 <= nums[i] <= 1000` |
| 38 | +* `1 <= words.length <= 2000` |
| 39 | +* `1 <= words[i].length <= 2000` |
| 40 | +* `words[i]` consists of lowercase English letters. |
| 41 | +* `letter` is a lowercase English letter. |
| 42 | +* At most <code>4 * 10<sup>4</sup></code> calls will be made to query. |
0 commit comments