-
Notifications
You must be signed in to change notification settings - Fork 0
/
172.py
53 lines (39 loc) · 1.56 KB
/
172.py
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Problem:
Given a string s and a list of words words, where each word is the same length, find
all starting indices of substrings in s that is a concatenation of every word in words
exactly once.
For example, given s = "dogcatcatcodecatdog" and words = ["cat", "dog"], return [0, 13],
since "dogcat" starts at index 0 and "catdog" starts at index 13.
Given s = "barfoobazbitbyte" and words = ["dog", "cat"], return [] since there are no
substrings composed of "dog" and "cat" in s.
The order of the indices does not matter.
"""
from itertools import permutations as generate_permutations
from re import finditer
from typing import Iterable, List
def concat_iteratable(iterateable: Iterable[str]) -> str:
concated_value = ""
for elem in iterateable:
concated_value += elem
return concated_value
def get_permutation_match_indices(s: str, words: List[str]) -> List[int]:
permutations = [
concat_iteratable(permutation)
for permutation in list(generate_permutations(words))
]
indices = []
for permutation in permutations:
indices.extend([match.start() for match in finditer(permutation, s)])
return indices
if __name__ == "__main__":
print(get_permutation_match_indices("barfoobazbitbyte", ["dog", "cat"]))
print(get_permutation_match_indices("dogcatcatcodecatdog", ["cat", "dog"]))
print(get_permutation_match_indices("dogcatcatcodecatdogcat", ["cat", "dog"]))
"""
SPECS:
TIME COMPLEXITY: O(2 ^ m + 2 ^ n)
SPACE COMPLEXITY: O(n)
[n = number of characters in input string
m = number of match words]
"""