@bdeng3 In order to ensure sufficient string length for the unfilled digit groups, each digit group should be at least filled with one digit. On each level, we have a remaining string of length len(s) and k groups to be filled.
As required,
len(s)  str_length_to_take_in_this _level >= k  1
(we need to deduct 1 (this digit group) from k)
So we have
1 <= str_len_to_take <= len(s)  (k  1)
(Each digit group should at least have one digit)
in python
range(1, len(s) (k1) + 1)
, which is range(len(s)k+1)
if you write i+1 in the loop content...
lilixu
@lilixu
144
Reputation
152
Posts
823
Profile views
4
Followers
0
Following
Posts made by lilixu

RE: DFS in Python

RE: DFS in Python
@bdeng3 We need to ensure that there are sufficient digits to fill all 4 digit groups and prevent the recursion entering those useless paths during dfs.
Suppose we have the input string '1111', we are going to only have '1' in our first digit group and in the first dfs level we are not going to have '11' or more because if we get '11' in first digit area, it is not possible for us to have sufficient digits to fill all four groups after that. 
Python 1 Line
return min(abs(xy) for x in [i for i, w in enumerate(words) if w == word1] for y in [i for i, w in enumerate(words) if w == word2])

Simple Clean Python
class Solution(object): def sortColors(self, nums): i = 0 for _ in range(len(nums)): if nums[i] == 0: nums.insert(nums.pop(i), 0) i += 1 elif nums[i] == 2: nums.append(nums.pop(i)) else: i += 1

Python Clean Solution
class Solution(object): def minWindow(self, s, t): table, match = {ch: t.count(ch) for ch in t}, len(t) ans, minlen = '', len(s) i, j = 0, 0 while j < len(s): if s[j] in table: table[s[j]] = 1 if table[s[j]] >= 0: match = 1 while match == 0: if s[i] in table: table[s[i]] += 1 if table[s[i]] > 0: match += 1 if ji+1 <= minlen: minlen, ans = ji+1, s[i:j+1] i += 1 j += 1 return ans