Amazon Online Assessment (OA) - Substrings of Size K with K-1 Distinct Characters
Find all unique substrings containing distinct characters of length k
given a string s
containing only lowercase alphabet characters.
Examples
Example 1:
Input: s
= xabxcd
, k
= 4
Output: ["abxc", "bxcd"]
Explanation:
The substrings are xabx
, abxc
, and bxcd
. However x
repeats in the xabx
, so it is not a valid substring of a distinct characters.
Example 2:
Input: s
= aabcdbcd
, k
= 3
Output: ["abc", "bcd", "cdb", "dbc"]
Explanation:
The substrings with distinct characters are abc
, bcd
, cdb
, dbc
, and again bcd
. However, we are looking for unique substrings, so we discard the last one which repeats.
Constraints:
k
is a positive number less than or equal to 26
.
Try it yourself
Solution
1 | 1 |
| |
2 | 2 | ||
3 | 3 |
| |
4 | - |
| |
4 | + |
| |
5 | - |
| |
5 | + |
| |
6 | 6 | ||
7 | 7 | ||
8 | + |
| |
9 | + |
| |
10 | + |
| |
11 | + |
| |
12 | + |
| |
13 | + |
| |
14 | + |
| |
15 | + |
| |
16 | + |
| |
17 | + |
| |
18 | + |
| |
19 | + |
| |
20 | + |
| |
21 | + | ||
22 | + |
| |
23 | + |
| |
24 | + |
| |
25 | + |
| |
26 | + |
| |
27 | + |
| |
28 | + |
| |
29 | + |
| |
30 | + | ||
31 | + |
| |
32 | + | ||
8 | 33 |
| |
9 | 34 |
| |
10 | 35 |
| |
11 | 36 |
| |
12 | 37 |
|
Loading full content...