@scurrlin/stencil 1.5.1 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,38 +14,114 @@ Whether you are studying for technical interviews, or just starting your coding
|
|
|
14
14
|
|
|
15
15
|
Most people when they attempt to memorize something study the full text and then attempt to regurgitate it on a blank page. Shocking, I know... but what if there was a step in between? What if memorization and pattern recognition weren't all or nothing games? This is where Stencil comes in.
|
|
16
16
|
|
|
17
|
-
Stencil is a language-agnostic memorization tool that strips code files down to their first letters while preserving spacing, capitalization, and punctuation. The "stencil" of the file is designed to act as a bridge between having something partially memorized and fully memorized. Below is an example of Stencil in action using LeetCode problem
|
|
17
|
+
Stencil is a language-agnostic memorization tool that strips code files down to their first letters while preserving spacing, capitalization, and punctuation. The "stencil" of the file is designed to act as a bridge between having something partially memorized and fully memorized. Below is an example of Stencil in action using LeetCode problem 30 "Substring with Concatenation of All Words":
|
|
18
18
|
|
|
19
19
|
## Example
|
|
20
20
|
|
|
21
21
|
Solution
|
|
22
22
|
|
|
23
23
|
```python
|
|
24
|
+
from collections import defaultdict
|
|
25
|
+
from typing import List
|
|
26
|
+
|
|
24
27
|
class Solution:
|
|
25
|
-
def
|
|
26
|
-
if not
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
|
|
29
|
+
if not s or not words:
|
|
30
|
+
return []
|
|
31
|
+
|
|
32
|
+
word_len = len(words[0])
|
|
33
|
+
count_words = len(words)
|
|
34
|
+
total_len = word_len * count_words
|
|
35
|
+
|
|
36
|
+
if len(s) < total_len:
|
|
37
|
+
return []
|
|
38
|
+
word_count = defaultdict(int)
|
|
39
|
+
for w in words:
|
|
40
|
+
word_count[w] += 1
|
|
41
|
+
result = []
|
|
42
|
+
|
|
43
|
+
for start_offset in range(word_len):
|
|
44
|
+
left = start_offset
|
|
45
|
+
matched_words = 0
|
|
46
|
+
seen_count = defaultdict(int)
|
|
47
|
+
for right in range(start_offset, len(s), word_len):
|
|
48
|
+
current_word = s[right : right + word_len]
|
|
49
|
+
if current_word in word_count:
|
|
50
|
+
seen_count[current_word] += 1
|
|
51
|
+
if seen_count[current_word] <= word_count[current_word]:
|
|
52
|
+
matched_words += 1
|
|
53
|
+
else:
|
|
54
|
+
while seen_count[current_word] > word_count[current_word]:
|
|
55
|
+
left_word = s[left : left + word_len]
|
|
56
|
+
seen_count[left_word] -= 1
|
|
57
|
+
if seen_count[left_word] < word_count[left_word]:
|
|
58
|
+
matched_words -= 1
|
|
59
|
+
left += word_len
|
|
60
|
+
if matched_words == count_words:
|
|
61
|
+
result.append(left)
|
|
62
|
+
left_word = s[left : left + word_len]
|
|
63
|
+
seen_count[left_word] -= 1
|
|
64
|
+
if seen_count[left_word] < word_count[left_word]:
|
|
65
|
+
matched_words -= 1
|
|
66
|
+
left += word_len
|
|
67
|
+
else:
|
|
68
|
+
seen_count.clear()
|
|
69
|
+
matched_words = 0
|
|
70
|
+
left = right + word_len
|
|
71
|
+
return result
|
|
34
72
|
```
|
|
35
73
|
|
|
36
74
|
Solution with Stencil
|
|
37
75
|
|
|
38
76
|
```python
|
|
77
|
+
f c i d
|
|
78
|
+
f t i L
|
|
79
|
+
|
|
39
80
|
c S:
|
|
40
|
-
d
|
|
41
|
-
i n n:
|
|
42
|
-
r
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
81
|
+
d f(s, s: s, w: L[s]) -> L[i]:
|
|
82
|
+
i n s o n w:
|
|
83
|
+
r []
|
|
84
|
+
|
|
85
|
+
w_l = l(w[0])
|
|
86
|
+
c_w = l(w)
|
|
87
|
+
t_l = w_l * c_w
|
|
88
|
+
|
|
89
|
+
i l(s) < t_l:
|
|
90
|
+
r []
|
|
91
|
+
w_c = d(i)
|
|
92
|
+
f w i w:
|
|
93
|
+
w_c[w] += 1
|
|
94
|
+
r = []
|
|
95
|
+
|
|
96
|
+
f s_o i r(w_l):
|
|
97
|
+
l = s_o
|
|
98
|
+
m_w = 0
|
|
99
|
+
s_c = d(i)
|
|
100
|
+
f r i r(s_o, l(s), w_l):
|
|
101
|
+
c_w = s[r : r + w_l]
|
|
102
|
+
i c_w i w_c:
|
|
103
|
+
s_c[c_w] += 1
|
|
104
|
+
i s_c[c_w] <= w_c[c_w]:
|
|
105
|
+
m_w += 1
|
|
106
|
+
e:
|
|
107
|
+
w s_c[c_w] > w_c[c_w]:
|
|
108
|
+
l_w = s[l : l + w_l]
|
|
109
|
+
s_c[l_w] -= 1
|
|
110
|
+
i s_c[l_w] < w_c[l_w]:
|
|
111
|
+
m_w -= 1
|
|
112
|
+
l += w_l
|
|
113
|
+
i m_w == c_w:
|
|
114
|
+
r.a(l)
|
|
115
|
+
l_w = s[l : l + w_l]
|
|
116
|
+
s_c[l_w] -= 1
|
|
117
|
+
i s_c[l_w] < w_c[l_w]:
|
|
118
|
+
m_w -= 1
|
|
119
|
+
l += w_l
|
|
120
|
+
e:
|
|
121
|
+
s_c.c()
|
|
122
|
+
m_w = 0
|
|
123
|
+
l = r + w_l
|
|
124
|
+
r r
|
|
49
125
|
```
|
|
50
126
|
|
|
51
127
|
## Local Installation
|