description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def is_unique(self, s):
return len(set(s)) == len(s)
def maxLength(self, arr: List[str]) -> int:
dct = {}
maxVal = 0
for i in range(0, len(arr)):
v = frozenset(arr[i])
if self.is_unique(arr[i]):
maxVal = max(maxVal, len(v))
for j in range(i + 1, len(arr)):
t = arr[i] + arr[j]
val = frozenset(t)
if self.is_unique(t):
arr.append(t)
maxVal = max(maxVal, len(val))
return maxVal | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
res = 0
def dfs(n, s, l, cur):
nonlocal res
if n == l:
if len(set(cur)) == len(cur):
res = max(res, len(cur))
return
for i in range(s, len(arr)):
new_cur = cur + arr[i]
if len(set(cur)) != len(cur):
return
dfs(n, i + 1, l + 1, new_cur)
for i in range(len(arr) + 1):
dfs(i, 0, 0, "")
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr) -> int:
maxLen = 0
arr = sorted(arr, key=lambda x: len(x), reverse=True)
self.compatible = {}
for i in range(len(arr)):
currString = arr[i]
if self.noDupes(currString):
if currString not in self.compatible:
self.compatible[currString] = []
for j in range(i + 1, len(arr)):
if self.noDupes(arr[j]) and self.uniqueChars(currString, arr[j]):
self.compatible[currString].append(arr[j])
for key in self.compatible:
if len(self.compatible[key]) == 0:
length = len(key)
else:
length = self.findLength(key, key, 0)
maxLen = max(maxLen, length)
return maxLen
def uniqueChars(self, s1, s2):
for c in s2:
if c in s1:
return False
return True
def noDupes(self, string):
counts = {}
for c in string:
counts[c] = counts.get(c, 0) + 1
if counts[c] > 1:
return False
return True
def findLength(self, currString, totalString, maxLen):
for string in self.compatible[currString]:
if self.uniqueChars(totalString, string):
maxLen = max(
maxLen,
self.findLength(
string, totalString + string, len(totalString) + len(string)
),
)
return maxLen | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
newarr = []
for w in arr:
if len(set(w)) == len(w):
newarr.append(w)
arr = newarr
n = len(arr)
res = 0
str_masks = [None] * n
for i in range(n):
wd = arr[i]
mask = 0
for ch in wd:
ch_pos = ord(ch) - ord("a")
mask |= 1 << ch_pos
str_masks[i] = mask
def dfs(idx, mask, curlen):
nonlocal res
if idx == n:
res = max(res, curlen)
return
if mask & str_masks[idx] == 0:
dfs(idx + 1, mask | str_masks[idx], curlen + len(arr[idx]))
dfs(idx + 1, mask, curlen)
dfs(0, 0, 0)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution(object):
def maxLength(self, A):
for i in range(len(A) - 1, -1, -1):
if len(set(A[i])) != len(A[i]):
A.pop(i)
N = len(A)
B = []
for word in A:
ct = [0] * 26
for letter in word:
ct[ord(letter) - ord("a")] += 1
B.append(ct)
self.ans = 0
count = [0] * 26
def search(i):
if i == N:
cand = sum(count)
if cand > self.ans:
self.ans = cand
return
for letter, ct in enumerate(B[i]):
if ct and count[letter]:
search(i + 1)
break
else:
search(i + 1)
for letter, ct in enumerate(B[i]):
if ct:
count[letter] += 1
search(i + 1)
for letter, ct in enumerate(B[i]):
if ct:
count[letter] -= 1
search(0)
return self.ans | CLASS_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
letters_dict = {}
def dfs(letter_set, arr, i):
if i >= len(arr):
return 0
if (tuple(letter_set), i) not in letters_dict:
if len(set(arr[i]).intersection(letter_set)) == 0 and len(
set(arr[i])
) == len(arr[i]):
letters_dict[tuple(letter_set), i] = max(
dfs(letter_set.union(set(arr[i])), arr, i + 1)
+ len(set(arr[i])),
dfs(letter_set, arr, i + 1),
)
else:
letters_dict[tuple(letter_set), i] = dfs(letter_set, arr, i + 1)
return letters_dict[tuple(letter_set), i]
return dfs(set(), arr, 0) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
arr.sort()
self.arr = arr
self.tmp = []
self.mlen = 0
self.len = 0
self.backtrack(0)
return self.mlen
def backtrack(self, pos):
if pos == len(self.arr):
if self.len > self.mlen:
self.mlen = self.len
return
self.backtrack(pos + 1)
lb = len(self.tmp)
self.tmp = self.tmp + list(self.arr[pos])
l = len(self.arr[pos])
if l == len(self.arr[pos]) and lb + l == len(set(self.tmp)):
self.len += l
self.backtrack(pos + 1)
self.len -= l
for i in range(l):
self.tmp.pop() | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def dfs(arr, path, res):
if self.checkUnique(path):
res.append(path)
for i in range(len(arr)):
dfs(arr[i + 1 :], path + arr[i], res)
concatenated_string, res = [], 0
dfs(arr, "", concatenated_string)
for elem in concatenated_string:
res = max(res, len(elem))
return res
def checkUnique(self, string):
dic = dict()
for ch in string:
if ch not in dic:
dic[ch] = 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR STRING VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def isUniqueChars(s):
return len(set(s)) == len(s)
arr = [set(w) for w in arr if isUniqueChars(w)]
results = list(arr)
for w in arr:
temp = []
for r in results:
if not w.intersection(r):
temp.append(w.union(r))
results = results + temp
max_length = 0
for w in results:
max_length = max(max_length, len(w))
return max_length | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
res = 0
arr = list([x for x in arr if len(set(x)) == len(x)])
arr = [set(x) for x in arr]
def dfs(n, s, l, cur):
nonlocal res
if n == l:
res = max(res, len(cur))
return
for i in range(s, len(arr)):
if len(cur.intersection(arr[i])) == 0:
dfs(n, i + 1, l + 1, cur.union(arr[i]))
for i in range(len(arr) + 1):
dfs(i, 0, 0, set())
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
dp = [set()]
for word in arr:
if len(word) != len(set(word)):
continue
curWord = set(word)
for used in dp[:]:
if len(used & curWord) == 0:
dp.append(used | curWord)
return len(max(dp, key=len)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def recurse(self, letters, arr, csum):
keys_as_str = "".join(sorted(letters))
if keys_as_str not in self.visited:
self.visited.add(keys_as_str)
self.max_len = max(self.max_len, csum)
for i, new_letters in enumerate(arr):
if len(new_letters.intersection(letters)) > 0:
continue
self.recurse(
new_letters.union(letters),
arr[:i] + arr[i + 1 :],
csum + len(new_letters),
)
def maxLength(self, arr: List[str]) -> int:
new_arr = []
for word in arr:
word_set = set(word)
if len(word_set) == len(word):
new_arr.append(word_set)
self.max_len = 0
self.visited = set()
for i, letters in enumerate(new_arr):
self.recurse(letters, new_arr[:i] + new_arr[i + 1 :], len(letters))
return self.max_len | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
dp = [set()]
length = 0
for word in arr:
if len(word) != len(set(word)):
continue
curr = set(word)
for used in dp[:]:
if not len(used & curr):
dp.append(used | curr)
length = max(length, len(used | curr))
return length | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def get_max_length(self, sets, idx, cur_set):
if idx == len(sets):
return len(cur_set)
m = 0
for i in range(idx, len(sets)):
if not cur_set & sets[i]:
res = self.get_max_length(sets, i + 1, cur_set.union(sets[i]))
else:
res = self.get_max_length(sets, i + 1, cur_set)
m = max(m, res)
return m
def maxLength(self, arr: List[str]) -> int:
sets = []
for i, s in enumerate(arr):
t = set()
for ch in s:
if ch in t:
break
else:
t.add(ch)
else:
sets.append(t)
print(sets)
return self.get_max_length(sets, 0, set()) | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | import itertools
class Solution:
def maxLength(self, arr):
def powerset(iterable):
import itertools
s = list(iterable)
return itertools.chain.from_iterable(
itertools.combinations(s, r) for r in range(len(s) + 1)
)
result = 0
for subset in powerset(arr):
candidate = "".join(subset)
if len(candidate) > result and len(candidate) == len(set(candidate)):
result = len(candidate)
return result | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IMPORT ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
filtered = []
for string in arr:
seen = {}
good = True
for letter in string:
if letter in seen:
good = False
else:
seen[letter] = True
if good:
filtered.append(string)
self.memo = {}
print(filtered)
return self.getLength(0, filtered, {})
def getLength(self, current, arr, used):
if not arr:
return current
if tuple(arr) in self.memo:
return self.memo[tuple(arr)]
maxi = current
for index in range(len(arr)):
string = arr[index]
possible = True
for char in string:
if char in used:
possible = False
if not possible:
continue
for char in string:
used[char] = True
result = self.getLength(
current + len(string), arr[:index] + arr[index + 1 :], used
)
if result > maxi:
maxi = result
for char in string:
del used[char]
self.memo[tuple(arr)] = maxi
return maxi | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR DICT VAR FUNC_DEF IF VAR RETURN VAR IF FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
ret = [set()]
for string in arr:
curr = set(string)
if len(curr) == len(string):
for seen in ret:
if not seen & curr:
ret.append(seen | curr)
max_len = 0
for string in ret:
max_len = max(max_len, len(string))
return max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
if len(arr) == 0:
return 0
currRes = []
for el in arr:
if self.isUnique(el):
currRes.append(el)
for el in arr:
for subSeq in currRes:
if self.isUnique(el + subSeq):
currRes.append(el + subSeq)
res = 0
for el in currRes:
res = max(res, len(el))
return res
def isUnique(self, s):
chars = set()
for c in s:
if c not in chars:
chars.add(c)
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.maxLen = 0
def isUnique(s):
return len(s) if len(s) == len(set(s)) else -1
def dfs(i, s):
if i == len(candi) and isUnique(s) > self.maxLen:
self.maxLen = len(s)
return
if i == len(candi):
return
dfs(i + 1, s)
dfs(i + 1, s + candi[i])
candi = []
for ss in arr:
if isUnique(ss) > 0:
candi.append(ss)
dfs(0, "")
return self.maxLen | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def is_unique(self, word):
count = dict()
for char in "".join(word):
if char in count:
return False
else:
count[char] = 1
return True
def find_max(self, arr, output, current, start_index):
if self.is_unique(current):
output.append(len("".join(current)))
for i in range(start_index, len(arr)):
self.find_max(arr, output, current + [arr[i]], i + 1)
def maxLength(self, arr: List[str]) -> int:
output = []
self.find_max(arr, output, [], 0)
return max(output) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL STRING VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
d = {}
def helper(banned, start):
if start == len(arr):
return len(banned)
k = tuple(sorted(str(banned) + str(start)))
if k in d:
return d[k]
string = arr[start]
new_set = set()
for i in string:
if i in banned or i in new_set:
return helper(banned, start + 1)
new_set.add(i)
new_banned = banned.union(new_set)
val = max(helper(new_banned, start + 1), helper(banned, start + 1))
d[k] = val
return val
initial_banned = set()
res = helper(initial_banned, 0)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
arr = [set(x) for x in arr if len(x) == len(set(x))]
mx = 0
def util(start, cur):
nonlocal mx
if start == len(arr):
mx = max(mx, len(cur))
for i in range(start, len(arr)):
if cur & arr[i]:
continue
cur |= arr[i]
util(i + 1, cur)
cur ^= arr[i]
mx = max(mx, len(cur))
util(0, set())
return mx | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
combinations = [set()]
for a in arr:
chars = set(a)
if len(chars) != len(a):
continue
for c in combinations:
if len(chars.intersection(c)) == 0:
combinations.append(chars.union(c))
return max([len(c) for c in combinations]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | from itertools import combinations
class Solution:
def maxLength(self, arr: List[str]) -> int:
max_count = 0
for i in range(0, len(arr) + 1):
comb = combinations(arr, i)
for c in comb:
count = 0
bag = set()
s = "".join(c)
for k in s:
if k in bag:
count = 0
break
else:
count += 1
bag.add(k)
max_count = max(max_count, count)
return max_count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def __init__(self):
self._maxLength = 0
def maxLength(self, arr: List[str]) -> int:
self.uniqueLengths(arr, [])
return self._maxLength
def uniqueLengths(self, arr, concat):
result = "".join(concat)
if not self.hasUniqueChars(result):
return
self._maxLength = max(self._maxLength, len(result))
for i in range(len(arr)):
concat.append(arr[i])
self.uniqueLengths(arr[i + 1 :], concat)
concat.pop()
def hasUniqueChars(self, concat):
counter = Counter(concat)
return all([(x == 1) for x in counter.values()]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR LIST RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr):
def isValid(chars):
if len(chars) == len(set(chars)):
return True
else:
return False
result = 0
currentSubset = set()
def iterate(idx):
nonlocal result, currentSubset
if idx == len(arr):
candidateSolution = "".join(currentSubset)
if isValid(candidateSolution) and len(candidateSolution) > result:
result = len(candidateSolution)
return
currentSubset.add(arr[idx])
iterate(idx + 1)
currentSubset.remove(arr[idx])
iterate(idx + 1)
iterate(0)
return result | CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def uniquelen(self, cur):
if len(cur) == len(set(cur)):
return len(cur)
else:
return -1
def dfs(self, arr, index, cur):
if index == len(arr) and self.uniquelen(cur) > self.ans:
self.ans = self.uniquelen(cur)
return
if index == len(arr):
return
self.dfs(arr, index + 1, cur)
self.dfs(arr, index + 1, cur + arr[index])
def maxLength(self, arr: List[str]) -> int:
self.ans = -1
self.dfs(arr, 0, "")
return self.ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
res = 0
unique = [""]
def isvalid(s):
return len(s) == len(set(s))
for word in arr:
for u in unique:
tmp = word + u
if isvalid(tmp):
unique.append(tmp)
res = max(res, len(tmp))
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
if arr == None:
return
path = []
result = [0]
def hasUniqueLetters(word):
hash_set = set()
for c in word:
if c in hash_set:
return False
else:
hash_set.add(c)
return True
def generateCombinations(arr, i, result, path):
if i == len(arr):
combination = "".join(path)
if path and hasUniqueLetters(combination):
if result[0] < len(combination):
result[0] = len(combination)
return
curr_path = []
curr_path.extend(path)
curr_path.append(arr[i])
generateCombinations(arr, i + 1, result, path)
generateCombinations(arr, i + 1, result, curr_path)
generateCombinations(arr, 0, result, path)
return result[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR NONE RETURN ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.output = []
self.res = 0
self.dfs(arr)
return self.res
def checkUnique(self, s):
m = set()
for i in s:
if i not in m:
m.add(i)
else:
return False
return True
def dfs(self, arr, first=0, curr=[]):
st = "".join(curr)
if self.checkUnique(st):
self.output.append(st)
self.res = max(len(st), self.res)
for i in range(first, len(arr)):
curr.append(arr[i])
self.dfs(arr, i + 1, curr)
curr.pop() | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF NUMBER LIST ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
if len(arr) == 1:
if len(set(arr[0])) == len(list(arr[0])):
return len(set(arr[0]))
else:
return 0
all_combinations = []
max_ = 0
for r in range(1, len(arr) + 1):
combinations_object = itertools.combinations(arr, r)
combinations_list = list(combinations_object)
for st in combinations_list:
comb = "".join(st)
if len(set(comb)) == len(comb):
max_ = max(max_, len(comb))
return max_ | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution(object):
def maxLength(self, arr):
def get_max_len(arr):
dp = [set(x) for x in arr if len(set(x)) == len(x)]
for v in arr:
if len((a := set(v))) == len(v):
for b in dp:
if a & b:
continue
dp.append(a | b)
return max(len(x) for x in dp) if dp else 0
return get_max_len(arr) | CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def score(s):
return sum(1 << ord(c) - ord("a") for c in s)
def valid(s):
return collections.Counter(s).most_common()[0][1] <= 1
arr = [s for s in arr if valid(s)]
s = [score(s) for s in arr]
res = 0
for i in range(2 ** len(s)):
val = 0
cur = 0
for j in range(len(s)):
if 1 << j & i == 0:
continue
if s[j] & val:
break
val |= s[j]
cur += len(arr[j])
else:
res = max(res, cur)
return res | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
n = len(arr)
self.max = 0
def helper(idx, chars):
if idx >= n:
if len(chars) == len(set(chars)):
self.max = max(self.max, len(chars))
else:
helper(idx + 1, chars)
helper(idx + 1, chars + list(arr[idx]))
helper(0, [])
return self.max | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
myQueue = collections.deque([("", 0)])
maxLen = 0
while myQueue:
word, start = myQueue.popleft()
for i in range(start, len(arr)):
newWord = word + arr[i]
if self.isUnique(newWord):
maxLen = max(maxLen, len(newWord))
myQueue.append((newWord, i + 1))
return maxLen
def isUnique(self, s):
return len(s) == len(set(s)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST STRING NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
return self.recursive(arr, "", 0)
def recursive(self, arr, curr_str, pick_idx):
if pick_idx == len(arr):
table = {}
for ch in curr_str:
if ch in table:
return 0
table[ch] = True
return len(curr_str)
return max(
self.recursive(arr, curr_str + arr[pick_idx], pick_idx + 1),
self.recursive(arr, curr_str, pick_idx + 1),
) | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR STRING NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
newarr = []
for cur in arr:
newarr.append(collections.Counter(cur))
res = [0]
def helper(cur, index):
if index == len(arr):
res[0] = max(res[0], len(cur))
else:
if len(arr[index]) == len(newarr[index]):
if not newarr[index] & cur:
helper(newarr[index] + cur, index + 1)
helper(cur, index + 1)
helper(collections.Counter(""), 0)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER RETURN VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
if not arr:
return 0
def isUnique(s):
return len(set(s)) == len(s)
def dfs(arr, res, curr):
if isUnique(curr):
res[0] = max(res[0], len(curr))
for i in range(len(arr)):
dfs(arr[i + 1 :], res, curr + arr[i])
res = [0]
dfs(arr, res, "")
return res[0] | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR STRING RETURN VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
uniqELements = [""]
maximum = 0
for i in range(len(arr)):
sz = len(uniqELements)
for j in range(sz):
x = arr[i] + uniqELements[j]
if len(x) == len(set(x)):
uniqELements.append(x)
maximum = max(maximum, len(x))
return maximum | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def helper(arr, substring, i):
unique_length = len(set(substring))
if len(substring) == unique_length:
self.max_len = max(self.max_len, unique_length)
if i == len(arr) - 1:
return
for j in range(i + 1, len(arr)):
helper(arr, substring + arr[j], j)
return
self.max_len = 0
helper(arr, "", -1)
return self.max_len | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
char_freq = {}
for i in range(len(arr)):
char_freq[i] = set()
for c in arr[i]:
if c not in char_freq[i]:
char_freq[i].add(c)
else:
del char_freq[i]
break
items_count = len(char_freq)
max_len = 0
for i in range(1, 2**items_count):
num = i
current_set = set()
current_len = 0
for j in list(char_freq.keys()):
intersect = current_set.intersection(char_freq[j])
if num & 1 and not intersect:
current_set.update(char_freq[j])
current_len += len(arr[j])
max_len = max(max_len, current_len)
elif num & 1 and intersect:
break
num >>= 1
return max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def valid(s, baskets):
return all([(c not in baskets) for c in s])
def find_max_len_recursive(baskets, currIdx):
if currIdx == len(arr):
return len(baskets)
res1 = float("-inf")
if valid(arr[currIdx], baskets):
res1 = find_max_len_recursive(
baskets.union(set(arr[currIdx])), currIdx + 1
)
res2 = find_max_len_recursive(baskets, currIdx + 1)
return max(res1, res2)
arr = [s for s in arr if len(s) == len(set(s))]
return find_max_len_recursive(set(), 0) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
N = len(arr)
charSets = [set()]
for s in arr:
currSet = set(s)
if len(currSet) != len(s):
continue
currSize = len(charSets)
for idx in range(currSize):
charSet = charSets[idx]
if charSet & currSet:
continue
charSets.append(charSet | currSet)
return max(len(charSet) for charSet in charSets) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.res = 0
def walk(i, temp):
self.res = max(self.res, len(temp))
for j in range(i, len(arr)):
if all(
v <= 1 for v in list(collections.Counter(temp + arr[j]).values())
):
walk(j + 1, temp + arr[j])
walk(0, "")
return self.res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
arr_sets = [set(x) for x in arr]
def buildSet(bitlist):
output = set()
for i, bit in enumerate(bitlist):
if bit:
output = output.union(arr_sets[i])
return output
def recurse(bitlist, index):
if index == len(arr):
return len(buildSet(bitlist))
new_bitlist = bitlist[:]
new_bitlist[index] = 0
not_included = recurse(new_bitlist, index + 1)
if len(arr_sets[index]) != len(arr[index]) or buildSet(
bitlist
).intersection(arr_sets[index]):
return not_included
else:
new_bitlist[index] = 1
return max(not_included, recurse(new_bitlist, index + 1))
return recurse([(0) for _ in arr], 0) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | def bo(i):
return ord(i) - ord("a")
class Solution:
def maxLength(self, arr: List[str]) -> int:
n = len(arr)
freq = []
ans = 0
for i in range(n):
s1 = set()
for j in arr[i]:
s1.add(bo(j))
if len(s1) == len(arr[i]):
freq.append(s1)
n = len(freq)
for i in range(1 << n):
s = set()
cnt = 0
for j in range(n):
if i & 1 << j:
for k in freq[j]:
s.add(k)
cnt += len(freq[j])
if cnt != len(s):
break
if cnt == len(s):
ans = max(ans, cnt)
if ans == 26:
break
return ans | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr):
prev = {(0): 0}
for word in arr:
seen = 0
duplicate = False
for char in word:
binaryC = 1 << ord(char) - ord("a")
if binaryC & seen != 0:
duplicate = True
break
else:
seen |= binaryC
if duplicate:
continue
toAdd = dict()
for k in prev:
if k & seen == 0:
toAdd[k | seen] = prev[k] + len(word)
prev.update(toAdd)
return max(prev.values())
class Solution:
def maxLength(self, arr):
cands = {(0): 0}
for word in arr:
if len(word) != len(set(word)):
continue
currW = sum(1 << ord(char) - ord("a") for char in word)
toAdd = dict()
for prevW in cands:
if prevW & currW == 0:
toAdd[prevW + currW] = cands[prevW] + len(word)
cands.update(toAdd)
return max(cands.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def is_unique(self, word):
letters = set()
for letter in word:
if letter in letters:
return False
letters.add(letter)
return True
def maxLength(self, arr: List[str]) -> int:
uniques = set()
for piece in arr:
next_uniques = set()
for unique in uniques:
concat = piece + unique
if self.is_unique(concat):
next_uniques.add(concat)
if self.is_unique(piece):
next_uniques.add(piece)
for u in next_uniques:
uniques.add(u)
if uniques:
return max([len(x) for x in uniques])
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
if len(arr) == 1:
return len(arr[0])
self.res = 0
def backtrack(start: int, path: set):
self.res = max(self.res, len(path))
if start == len(arr):
return
for i in range(start, len(arr)):
new_chars = set(arr[i])
if len(new_chars) != len(arr[i]):
continue
if len(new_chars & path) == 0:
new_path = new_chars | path
backtrack(i + 1, new_path)
backtrack(0, set())
return self.res | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def digit_representation(s):
ans = 0
for c in s:
ans |= 1 << ord(c) - ord("a")
return ans
A = sorted(
[
(len(s), digit_representation(s))
for s in set(arr)
if len(set(s)) == len(s)
],
reverse=True,
)
if not A:
return 0
R = [sum(t[0] for t in A)]
for i in range(1, len(A)):
R.append(R[-1] - A[i][0])
self.ans = A[0][0]
def helper(i, b, k):
if i == len(A):
self.ans = max(self.ans, k)
elif k + R[i] > self.ans:
if not b & A[i][1]:
helper(i + 1, b | A[i][1], k + A[i][0])
helper(i + 1, b, k)
helper(0, 0, 0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.ans = 0
vis = set()
def help(s, i):
if len(s) == len(set(s)):
self.ans = max(self.ans, len(s))
if i >= len(arr):
return
for j in range(i, len(arr)):
if j not in vis:
vis.add(j)
help(s + arr[j], j + 1)
vis.remove(j)
help("", 0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING NUMBER RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.out = set()
self.visited = set()
opts = []
for sub in arr:
tmp = set()
omit = False
for ch in sub:
if ch in tmp:
omit = True
tmp.add(ch)
if not omit:
opts.append(tmp)
self.dfs(set(), opts)
return len(self.out)
def dfs(self, curr, opts):
if tuple(sorted(curr)) in self.visited:
return
self.visited.add(tuple(sorted(curr)))
if len(curr) > len(self.out):
self.out = curr
if not opts:
return
for j, opt in enumerate(opts):
if not curr.intersection(opt):
self.dfs(curr.union(opt), opts[:j] + opts[j + 1 :]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def isUnique(concatString):
concatSet = set()
for char in concatString:
if char in concatSet:
return False
concatSet.add(char)
return True
length = len(arr)
self.result = 0
def dfs(index, curr):
if isUnique(curr):
self.result = max(self.result, len(curr))
if index == length - 1:
return
for i in range(index + 1, length):
dfs(i, curr + arr[i])
for i in range(length):
dfs(i, arr[i])
return self.result | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def maxLength(i, string):
nonlocal ans
if i == len(arr):
s, invalid = set(), False
for ch in string:
if ch in s:
invalid = True
break
s.add(ch)
if not invalid:
ans = max(ans, len(string))
return
maxLength(i + 1, string)
maxLength(i + 1, string + arr[i])
ans = 0
maxLength(0, "")
return ans | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
def unique_char_count(s):
chars = set()
for char in s:
if char in chars:
return -1
else:
chars.add(char)
return len(s)
def max_unique(arr, index, cur_str, result):
if index >= len(arr):
if unique_char_count(cur_str) > result[0]:
result[0] = len(cur_str)
return
max_unique(arr, index + 1, cur_str + arr[index], result)
max_unique(arr, index + 1, cur_str, result)
result = [0]
max_unique(arr, 0, "", result)
return result[0] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING VAR RETURN VAR NUMBER VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
count = 0
def dfs(self, arr, i, visited):
for index in range(i + 1, len(arr)):
visit = True
for j in range(0, len(arr[index])):
if arr[index][j] in visited:
for k in range(0, j):
visited.remove(arr[index][k])
visit = False
break
else:
visited.add(arr[index][j])
if visit:
self.count = max(self.count, len(visited))
self.dfs(arr, index, visited)
for char in arr[index]:
if char in visited and visit:
visited.remove(char)
def maxLength(self, arr: List[str]) -> int:
if len(arr) == 0:
return 0
visited = set()
self.count = 0
self.dfs(arr, -1, visited)
return self.count | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
a = [set(i) for i in arr if len(set(i)) == len(i)]
n = len(a)
an = 0
for v in range(1, 2**n):
s = set()
i = 0
while v > 0:
if v & 1 == 1:
if s & a[i]:
break
s = s ^ a[i]
v = v >> 1
i += 1
an = max(an, len(s))
return an | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
d = [""]
for a in arr:
if len(set(a)) < len(a):
continue
else:
for x in d:
if set(x) & set(a):
continue
else:
t = set(a) | set(x)
d.append(t)
max = 0
for i in d:
if len(i) > max:
max = len(i)
return max | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING FOR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
dp = defaultdict(lambda: set())
def dfs(index, cur):
if index == len(arr) or cur in dp[index]:
return len(cur)
dp[index].add(cur)
best = len(cur)
for x in range(index, len(arr)):
count = Counter(cur)
xCount = Counter(arr[x])
canTake = True
for k, v in xCount.items():
if k in count or v > 1:
canTake = False
if canTake:
best = max(best, dfs(x + 1, cur + arr[x]))
best = max(best, dfs(x + 1, cur))
return best
return dfs(0, "") | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER STRING VAR |
Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters.
Return the maximum possible length of s.
Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".
Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Constraints:
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lower case English letters. | class Solution:
def maxLength(self, arr: List[str]) -> int:
self.array = arr
return max(self.recurse("", set(), 0, True), self.recurse("", set(), 0, False))
def recurse(self, prefix: str, letters: set, index: int, include: bool):
if index >= len(self.array):
return len(prefix)
if include:
if len(self.array[index]) != len(set(self.array[index])):
return len(prefix)
if any(letter in letters for letter in self.array[index]):
return len(prefix)
return max(
self.recurse(
"".join((prefix, self.array[index])),
letters | set(self.array[index]),
index + 1,
True,
),
self.recurse(
"".join((prefix, self.array[index])),
letters | set(self.array[index]),
index + 1,
False,
),
)
return max(
self.recurse(prefix, letters, index + 1, True),
self.recurse(prefix, letters, index + 1, False),
) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER |
Childan is making up a legendary story and trying to sell his forgery — a necklace with a strong sense of "Wu" to the Kasouras. But Mr. Kasoura is challenging the truth of Childan's story. So he is going to ask a few questions about Childan's so-called "personal treasure" necklace.
This "personal treasure" is a multiset S of m "01-strings".
A "01-string" is a string that contains n characters "0" and "1". For example, if n=4, strings "0110", "0000", and "1110" are "01-strings", but "00110" (there are 5 characters, not 4) and "zero" (unallowed characters) are not.
Note that the multiset S can contain equal elements.
Frequently, Mr. Kasoura will provide a "01-string" t and ask Childan how many strings s are in the multiset S such that the "Wu" value of the pair (s, t) is not greater than k.
Mrs. Kasoura and Mr. Kasoura think that if s_i = t_i (1≤ i≤ n) then the "Wu" value of the character pair equals to w_i, otherwise 0. The "Wu" value of the "01-string" pair is the sum of the "Wu" values of every character pair. Note that the length of every "01-string" is equal to n.
For example, if w=[4, 5, 3, 6], "Wu" of ("1001", "1100") is 7 because these strings have equal characters only on the first and third positions, so w_1+w_3=4+3=7.
You need to help Childan to answer Mr. Kasoura's queries. That is to find the number of strings in the multiset S such that the "Wu" value of the pair is not greater than k.
Input
The first line contains three integers n, m, and q (1≤ n≤ 12, 1≤ q, m≤ 5⋅ 10^5) — the length of the "01-strings", the size of the multiset S, and the number of queries.
The second line contains n integers w_1, w_2, …, w_n (0 ≤ w_i ≤ 100) — the value of the i-th caracter.
Each of the next m lines contains the "01-string" s of length n — the string in the multiset S.
Each of the next q lines contains the "01-string" t of length n and integer k (0≤ k≤ 100) — the query.
Output
For each query, print the answer for this query.
Examples
Input
2 4 5
40 20
01
01
10
11
00 20
00 40
11 20
11 40
11 60
Output
2
4
2
3
4
Input
1 2 4
100
0
1
0 0
0 100
1 0
1 100
Output
1
2
1
2
Note
In the first example, we can get:
"Wu" of ("01", "00") is 40.
"Wu" of ("10", "00") is 20.
"Wu" of ("11", "00") is 0.
"Wu" of ("01", "11") is 20.
"Wu" of ("10", "11") is 40.
"Wu" of ("11", "11") is 60.
In the first query, pairs ("11", "00") and ("10", "00") satisfy the condition since their "Wu" is not greater than 20.
In the second query, all strings satisfy the condition.
In the third query, pairs ("01", "11") and ("01", "11") satisfy the condition. Note that since there are two "01" strings in the multiset, the answer is 2, not 1.
In the fourth query, since k was increased, pair ("10", "11") satisfies the condition too.
In the fifth query, since k was increased, pair ("11", "11") satisfies the condition too. | from sys import stdin
def main():
n, m, q = map(int, input().split())
c = sz = 1 << n
costs, cnt = [0] * sz, [0] * sz
for w in map(int, input().split()):
c //= 2
for i in range(c, sz, c * 2):
for j in range(i, i + c):
costs[j] += w
for i, c in enumerate(costs):
if c > 101:
costs[i] = 101
costs.reverse()
l = stdin.read().splitlines()
for s in l[:m]:
cnt[int(s, 2)] += 1
del l[:m]
cache = []
for i in range(sz):
row = [0] * 102
cache.append(row)
for j, c in enumerate(cnt):
row[costs[i ^ j]] += c
a = 0
for j, b in enumerate(row):
a += b
row[j] = a
for i, s in enumerate(l):
row = s.split()
l[i] = str(cache[int(row[0], 2)][int(row[1])])
print("\n".join(l))
main() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
Childan is making up a legendary story and trying to sell his forgery — a necklace with a strong sense of "Wu" to the Kasouras. But Mr. Kasoura is challenging the truth of Childan's story. So he is going to ask a few questions about Childan's so-called "personal treasure" necklace.
This "personal treasure" is a multiset S of m "01-strings".
A "01-string" is a string that contains n characters "0" and "1". For example, if n=4, strings "0110", "0000", and "1110" are "01-strings", but "00110" (there are 5 characters, not 4) and "zero" (unallowed characters) are not.
Note that the multiset S can contain equal elements.
Frequently, Mr. Kasoura will provide a "01-string" t and ask Childan how many strings s are in the multiset S such that the "Wu" value of the pair (s, t) is not greater than k.
Mrs. Kasoura and Mr. Kasoura think that if s_i = t_i (1≤ i≤ n) then the "Wu" value of the character pair equals to w_i, otherwise 0. The "Wu" value of the "01-string" pair is the sum of the "Wu" values of every character pair. Note that the length of every "01-string" is equal to n.
For example, if w=[4, 5, 3, 6], "Wu" of ("1001", "1100") is 7 because these strings have equal characters only on the first and third positions, so w_1+w_3=4+3=7.
You need to help Childan to answer Mr. Kasoura's queries. That is to find the number of strings in the multiset S such that the "Wu" value of the pair is not greater than k.
Input
The first line contains three integers n, m, and q (1≤ n≤ 12, 1≤ q, m≤ 5⋅ 10^5) — the length of the "01-strings", the size of the multiset S, and the number of queries.
The second line contains n integers w_1, w_2, …, w_n (0 ≤ w_i ≤ 100) — the value of the i-th caracter.
Each of the next m lines contains the "01-string" s of length n — the string in the multiset S.
Each of the next q lines contains the "01-string" t of length n and integer k (0≤ k≤ 100) — the query.
Output
For each query, print the answer for this query.
Examples
Input
2 4 5
40 20
01
01
10
11
00 20
00 40
11 20
11 40
11 60
Output
2
4
2
3
4
Input
1 2 4
100
0
1
0 0
0 100
1 0
1 100
Output
1
2
1
2
Note
In the first example, we can get:
"Wu" of ("01", "00") is 40.
"Wu" of ("10", "00") is 20.
"Wu" of ("11", "00") is 0.
"Wu" of ("01", "11") is 20.
"Wu" of ("10", "11") is 40.
"Wu" of ("11", "11") is 60.
In the first query, pairs ("11", "00") and ("10", "00") satisfy the condition since their "Wu" is not greater than 20.
In the second query, all strings satisfy the condition.
In the third query, pairs ("01", "11") and ("01", "11") satisfy the condition. Note that since there are two "01" strings in the multiset, the answer is 2, not 1.
In the fourth query, since k was increased, pair ("10", "11") satisfies the condition too.
In the fifth query, since k was increased, pair ("11", "11") satisfies the condition too. | import sys
n, m, q = map(int, sys.stdin.readline().strip().split())
w = [int(x) for x in sys.stdin.readline().strip().split()]
mx = pow(2, n)
mask = mx - 1
sn = [0] * mx
for i in range(m):
s = sys.stdin.readline().strip()
si = int(s, 2)
sn[si] += 1
wu = [0] * mx
for i in range(mx):
for j in range(n):
if 1 << j & i != 0:
wu[i] += w[n - j - 1]
c = []
for i in range(mx):
c.append([0] * 102)
for j in range(mx):
join = (~i ^ j) & mask
val = wu[join]
if val > 100:
val = 101
c[i][val] += sn[j]
for j in range(1, 102):
c[i][j] += c[i][j - 1]
for i in range(q):
row = sys.stdin.readline().strip().split()
t = int(row[0], 2)
k = int(row[1])
sys.stdout.write(str(c[t][k]) + "\n") | IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR STRING |
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
] | class Solution:
def subsets(self, nums):
subsets = [[]]
for v in nums:
extra = []
for s in subsets:
extra.append(s + [v])
subsets += extra
return subsets | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR RETURN VAR |
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
] | class Solution:
def subsets(self, nums):
result = [[]]
for num in nums:
result += self.get_subset(result, num)
return result
def get_subset(self, subsets, item):
new_subset_with_item = []
for subset in subsets:
new_subset_with_item.append(subset + [item])
return new_subset_with_item | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST FOR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR RETURN VAR |
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
] | class Solution:
def subsets(self, nums):
def mask(x):
return 1 << x
exp = 1 << len(nums)
ans = []
for count in range(exp):
new = []
i = 0
while mask(i) <= count:
if mask(i) & count:
new.append(nums[i])
i += 1
ans.append(new)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF RETURN BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
self.ans, empty = 0, 0
m, n = len(grid[0]), len(grid)
def dfs(x, y, rest):
if x < 0 or x >= n or y < 0 or y >= m or grid[x][y] < 0:
return
if grid[x][y] == 2 and rest == 0:
self.ans += 1
return
neibs = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for dx, dy in neibs:
tmp = grid[x][y]
grid[x][y] = -2
dfs(x + dx, y + dy, rest - 1)
grid[x][y] = tmp
for i, j in product(list(range(n)), list(range(m))):
if grid[i][j] == 1:
sx, sy = i, j
empty += grid[i][j] != -1
dfs(sx, sy, empty - 1)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
non_obstacles = 0
start_row, start_col = 0, 0
for row in range(0, rows):
for col in range(0, cols):
cell = grid[row][col]
if cell >= 0:
non_obstacles += 1
if cell == 1:
start_row, start_col = row, col
path_count = 0
def backtrack(row, col, remain):
nonlocal path_count
if grid[row][col] == 2 and remain == 1:
path_count += 1
return
temp = grid[row][col]
grid[row][col] = -4
remain -= 1
for ro, co in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
next_row, next_col = row + ro, col + co
if not (0 <= next_row < rows and 0 <= next_col < cols):
continue
if grid[next_row][next_col] < 0:
continue
backtrack(next_row, next_col, remain)
grid[row][col] = temp
backtrack(start_row, start_col, non_obstacles)
return path_count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
def traverse(row, col, spacesLeft):
if spacesLeft == 1 and grid[row][col] == 2:
self.results += 1
return
temp = grid[row][col]
grid[row][col] = "#"
spacesLeft -= 1
for rowOffset, colOffset in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
nextRow, nextCol = row + rowOffset, col + colOffset
if nextRow < 0 or nextRow == rows or nextCol < 0 or nextCol == cols:
continue
if grid[nextRow][nextCol] == -1 or grid[nextRow][nextCol] == "#":
continue
traverse(nextRow, nextCol, spacesLeft)
grid[row][col] = temp
rows, cols = len(grid), len(grid[0])
spaces = 0
for row in range(rows):
for col in range(cols):
if grid[row][col] >= 0:
spaces += 1
if grid[row][col] == 1:
start = row, col
self.results = 0
traverse(start[0], start[1], spaces)
return self.results | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR NUMBER FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
def dfs(i, j, out, to_visit):
if (
i < 0
or j < 0
or i >= len(grid)
or j >= len(grid[0])
or grid[i][j] == "#"
or grid[i][j] == -1
):
return
if grid[i][j] == 2 and to_visit == 0:
out[0] += 1
else:
tmp = grid[i][j]
grid[i][j] = "#"
dfs(i, j + 1, out, to_visit - 1)
dfs(i, j - 1, out, to_visit - 1)
dfs(i + 1, j, out, to_visit - 1)
dfs(i - 1, j, out, to_visit - 1)
grid[i][j] = tmp
res = [0]
ori, orj = 0, 0
to_visit = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
ori, orj = i, j
if grid[i][j] != -1:
to_visit += 1
dfs(ori, orj, res, to_visit - 1)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR STRING VAR VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
def fun(g, i0, i1, n1, t1, f2, row, col):
if n1 == t1:
if abs(f2[1] - i1) + abs(f2[0] - i0) == 1:
self.ans += 1
else:
if i1 + 1 != col and g[i0][i1 + 1] == 0:
g[i0][i1 + 1] = 1
fun(g, i0, i1 + 1, n1 + 1, t1, f2, row, col)
g[i0][i1 + 1] = 0
if i1 != 0 and g[i0][i1 - 1] == 0:
g[i0][i1 - 1] = 1
fun(g, i0, i1 - 1, n1 + 1, t1, f2, row, col)
g[i0][i1 - 1] = 0
if i0 + 1 != row and g[i0 + 1][i1] == 0:
g[i0 + 1][i1] = 1
fun(g, i0 + 1, i1, n1 + 1, t1, f2, row, col)
g[i0 + 1][i1] = 0
if i0 != 0 and g[i0 - 1][i1] == 0:
g[i0 - 1][i1] = 1
fun(g, i0 - 1, i1, n1 + 1, t1, f2, row, col)
g[i0 - 1][i1] = 0
self.ans = 0
i0, i1, t1 = 0, 0, 0
f2 = [0, 0]
row = len(grid)
col = len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == 0:
t1 += 1
elif grid[i][j] == 1:
i0, i1 = i, j
elif grid[i][j] == 2:
f2 = [i, j]
fun(grid, i0, i1, 0, t1, f2, row, col)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
def dfs(r, c, n):
if r < 0 or r >= R or c < 0 or c >= C or grid[r][c] == -1:
return 0
if grid[r][c] == 2:
return n == 0
grid[r][c] = -1
paths = (
dfs(r + 1, c, n - 1)
+ dfs(r - 1, c, n - 1)
+ dfs(r, c + 1, n - 1)
+ dfs(r, c - 1, n - 1)
)
grid[r][c] = 0
return paths
R = len(grid)
C = len(grid[0])
n = 1
sr, sc = -1, -1
for r in range(R):
for c in range(C):
if grid[r][c] == 0:
n += 1
elif grid[r][c] == 1:
sr, sc = r, c
return dfs(sr, sc, n) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
self.ans, self.count = 0, 0
def go(cur, seen):
i, j = cur
if grid[i][j] == 2:
if len(seen) == self.count:
self.ans += 1
else:
return
for r, c in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
if (
0 <= r < len(grid)
and 0 <= c < len(grid[0])
and (r, c) not in seen
and grid[r][c] != -1
):
seen.add((r, c))
go((r, c), seen)
seen.remove((r, c))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] in [0, 1, 2]:
self.count += 1
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
go((i, j), {(i, j)})
return self.ans
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR LIST NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
nr = len(grid)
nc = len(grid[0])
all_cells = 0
start = 0, 0
res = 0
direction = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for r in range(nr):
for c in range(nc):
if grid[r][c] >= 0:
all_cells += 1
if grid[r][c] == 1:
start = r, c
def dfs(r, c, cell_nums):
nonlocal res
nonlocal direction
nonlocal grid
nonlocal nr
nonlocal nc
if grid[r][c] == 2 and cell_nums == 1:
res += 1
return
temp = grid[r][c]
grid[r][c] = -2
cell_nums -= 1
for d in direction:
rr = r + d[0]
cc = c + d[1]
if 0 <= rr < nr and 0 <= cc < nc and grid[rr][cc] >= 0:
dfs(rr, cc, cell_nums)
grid[r][c] = temp
dfs(*start, all_cells)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
self.ret = 0
empty = 0
m = len(grid)
n = len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
sx = i
sy = j
if grid[i][j] >= 0:
empty += 1
def backtrack(grid, x, y, num_left):
if x < 0 or y < 0 or x >= m or y >= n or grid[x][y] < 0:
return
if grid[x][y] == 2 and num_left == 0:
self.ret += 1
return
neighs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
for dx, dy in neighs:
orig_value = grid[x][y]
grid[x][y] = -1
backtrack(grid, x + dx, y + dy, num_left - 1)
grid[x][y] = orig_value
backtrack(grid, sx, sy, empty - 1)
return self.ret | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
start_cell, end_cell = None, None
empty_cells = {}
for i in range(rows):
for j in range(cols):
if grid[i][j] in [0, 1]:
empty_cells[i, j] = set()
if grid[i][j] == 1:
start_cell = i, j
elif grid[i][j] == 2:
end_cell = i, j
for x, y in empty_cells:
for xch, ych in directions:
neighbor = x + xch, y + ych
if neighbor in empty_cells:
empty_cells[x, y].add(neighbor)
next_to_finish = set(
(end_cell[0] + xch, end_cell[1] + ych) for xch, ych in directions
)
visited = set([start_cell])
def count_routes(cell=start_cell):
left_to_visit = len(empty_cells) - len(visited)
if not left_to_visit:
return int(cell in next_to_finish)
routes_to_end = 0
for neighbor in empty_cells[cell] - visited:
visited.add(neighbor)
routes_to_end += count_routes(neighbor)
visited.remove(neighbor)
return routes_to_end
return count_routes() | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
self.ans = 0
m, n = len(grid), len(grid[0])
candidates = set()
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
si, sj = i, j
elif grid[i][j] == 0 or grid[i][j] == 2:
candidates.add((i, j))
def helper(i, j):
if grid[i][j] == 2:
if not candidates:
self.ans += 1
return
if grid[i][j] == -1:
return
if i < m - 1 and (i + 1, j) in candidates:
candidates.remove((i + 1, j))
helper(i + 1, j)
candidates.add((i + 1, j))
if i > 0 and (i - 1, j) in candidates:
candidates.remove((i - 1, j))
helper(i - 1, j)
candidates.add((i - 1, j))
if j < n - 1 and (i, j + 1) in candidates:
candidates.remove((i, j + 1))
helper(i, j + 1)
candidates.add((i, j + 1))
if j > 0 and (i, j - 1) in candidates:
candidates.remove((i, j - 1))
helper(i, j - 1)
candidates.add((i, j - 1))
helper(si, sj)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN IF VAR VAR VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
count = 0
visited = set()
def dfs(i: int, j: int):
nonlocal count
nonlocal num_empty
if grid[i][j] == -1 or (i, j) in visited:
return
elif grid[i][j] == 2 and len(visited) == num_empty + 1:
count += 1
return
else:
visited.add((i, j))
if i > 0:
dfs(i - 1, j)
if j > 0:
dfs(i, j - 1)
if i < len(grid) - 1:
dfs(i + 1, j)
if j < len(grid[0]) - 1:
dfs(i, j + 1)
visited.remove((i, j))
start_i = start_j = None
num_empty = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
start_i, start_j = i, j
elif grid[i][j] == 0:
num_empty += 1
dfs(start_i, start_j)
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR RETURN IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
start = end = None
cnt = ans = 0
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
start = i, j
elif grid[i][j] == 1:
end = i, j
if grid[i][j] != -1:
cnt += 1
def dfs(i, j, cnt):
nonlocal m, n, ans
if grid[i][j] == 2 and cnt == 1:
ans += 1
return
val = grid[i][j]
grid[i][j] = -1
for x, y in map(
lambda t: (t[0] + i, t[1] + j), [(-1, 0), (1, 0), (0, -1), (0, 1)]
):
if 0 <= x < m and 0 <= y < n and grid[x][y] != -1:
dfs(x, y, cnt - 1)
grid[i][j] = val
dfs(*start, cnt)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
start = end = None
remain = 0
for m in range(len(grid)):
for n in range(len(grid[0])):
if grid[m][n] == 1:
start = m, n
elif grid[m][n] == 2:
end = m, n
if grid[m][n] == 0:
remain += 1
if start is None or end is None:
return 0
return self._helper(grid, start[0], start[1], end[0], end[1], remain + 1)
def _helper(
self,
grid: List[List[int]],
start_m: int,
start_n: int,
end_m: int,
end_n: int,
remain: int,
) -> int:
if (
0 <= start_m < len(grid)
and 0 <= start_n < len(grid[0])
and grid[start_m][start_n] > -1
):
if remain < 0:
return 0
elif remain == 0 and start_m == end_m and start_n == end_n:
return 1
count = 0
grid[start_m][start_n] = -2
count += self._helper(grid, start_m + 1, start_n, end_m, end_n, remain - 1)
count += self._helper(grid, start_m, start_n + 1, end_m, end_n, remain - 1)
count += self._helper(grid, start_m - 1, start_n, end_m, end_n, remain - 1)
count += self._helper(grid, start_m, start_n - 1, end_m, end_n, remain - 1)
grid[start_m][start_n] = 0
return count
return 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NONE VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid):
maxPath = 2
neighbors = [None] * (len(grid) * len(grid[0]))
visited = [False] * (len(grid) * len(grid[0]))
for x in range(len(grid)):
for y in range(len(grid[0])):
node = x * len(grid[0]) + y
if grid[x][y] == 0:
neighbors[node] = getNeighbors(grid, x, y)
visited[node] = False
maxPath += 1
elif grid[x][y] == 1:
start = node
neighbors[node] = getNeighbors(grid, x, y)
elif grid[x][y] == 2:
end = node
visited[node] = False
return count(neighbors, visited, start, maxPath, 1, end)
def getNeighbors(grid, x, y):
output = []
if x > 0 and grid[x - 1][y] % 2 == 0:
output.append((x - 1) * len(grid[0]) + y)
if x < len(grid) - 1 and grid[x + 1][y] % 2 == 0:
output.append((x + 1) * len(grid[0]) + y)
if y > 0 and grid[x][y - 1] % 2 == 0:
output.append(x * len(grid[0]) + y - 1)
if y < len(grid[0]) - 1 and grid[x][y + 1] % 2 == 0:
output.append(x * len(grid[0]) + y + 1)
return output
def count(neighbors, visited, node, maxPath, pathLength, destination):
if pathLength == maxPath and node == destination:
return 1
elif pathLength == maxPath or node == destination:
return 0
visited[node] = True
sum = 0
for k in neighbors[node]:
if not visited[k]:
sum += count(neighbors, visited, k, maxPath, pathLength + 1, destination)
visited[node] = False
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, A: List[List[int]]) -> int:
self.res = 0
rows = len(A)
cols = len(A[0])
empty = 1
for i in range(rows):
for j in range(cols):
if A[i][j] == 1:
x, y = i, j
elif A[i][j] == 2:
end = i, j
elif A[i][j] == 0:
empty += 1
self.dfs(A, x, y, empty, end)
return self.res
def dfs(self, A, x, y, empty, end):
if x < 0 or x >= len(A) or y < 0 or y >= len(A[0]) or A[x][y] < 0:
return
if (x, y) == end:
self.res += empty == 0
return
A[x][y] = -2
self.dfs(A, x + 1, y, empty - 1, end)
self.dfs(A, x - 1, y, empty - 1, end)
self.dfs(A, x, y + 1, empty - 1, end)
self.dfs(A, x, y - 1, empty - 1, end)
A[x][y] = 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER |
On a 2-dimensional grid, there are 4 types of squares:
1 represents the starting square. There is exactly one starting square.
2 represents the ending square. There is exactly one ending square.
0 represents empty squares we can walk over.
-1 represents obstacles that we cannot walk over.
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
Example 1:
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
Example 2:
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
Example 3:
Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.
Note:
1 <= grid.length * grid[0].length <= 20 | class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
def dfs(i, j, N):
if N == -1 and grid[i][j] == 2:
return 1
grid[i][j] = -1
res = sum(
[
dfs(row, col, N - 1)
for x, y in dirs
for row, col in [(i + x, j + y)]
if 0 <= row < m
and 0 <= col < n
and (N > 0 and not grid[row][col] or N == 0 and grid[row][col] == 2)
]
)
grid[i][j] = 0
return res
m, n = len(grid), len(grid[0])
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
N = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
start = [i, j]
elif not grid[i][j]:
N += 1
return dfs(start[0], start[1], N) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
tot = sum(nums)
if k <= 0 or tot % k != 0:
return False
visited = [0] * len(nums)
def canPart(nums, visited, idx, curSum, k, target):
if k == 1:
return True
if curSum == target:
return canPart(nums, visited, 0, 0, k - 1, target)
if curSum > target:
return False
for i in range(idx, len(nums)):
if not visited[i]:
visited[i] = 1
if canPart(nums, visited, i + 1, curSum + nums[i], k, target):
return True
visited[i] = 0
return False
return canPart(nums, visited, 0, 0, k, tot / k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
if not nums:
return True
if sum(nums) % k != 0:
return False
target = sum(nums) / k
nums.sort()
if nums[-1] > target:
return False
while nums and nums[-1] == target:
nums.pop()
k -= 1
def partition(nums, subsets, target):
if not nums:
return True
selected = nums.pop()
for i in range(len(subsets)):
if subsets[i] + selected <= target:
subsets[i] += selected
if partition(nums, subsets, target):
return True
subsets[i] -= selected
if subsets[i] == 0:
break
nums.append(selected)
return False
return partition(nums, subsets=[0] * k, target=target) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
quotient = sum(nums) / k
if quotient % 1 != 0:
return False
for num in nums:
if num > quotient:
return False
if num == quotient:
nums.remove(quotient)
k -= 1
nums.sort(reverse=True)
self.nums = nums
self.quotient = quotient
print(self.nums)
answer = self.dfs([0] * len(nums), 0, k)
return answer
def dfs(self, visit, accu, k):
if k == 1:
return True
for i in range(len(self.nums)):
if not visit[i]:
if self.nums[i] + accu < self.quotient:
visit[i] = 1
if self.dfs(visit, self.nums[i] + accu, k):
return True
visit[i] = 0
if self.nums[i] + accu == self.quotient:
visit[i] = 1
accu = self.nums[i] + accu
return self.dfs(visit, 0, k - 1)
return False | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
total = sum(nums)
if total % k != 0:
return False
target = total / k
used = [False] * len(nums)
nums = sorted(nums)
def check(nums, k, cur, pos):
if k == 1:
return True
for i in range(pos, -1, -1):
if not used[i]:
if nums[i] + cur < target:
used[i] = True
if check(nums, k, cur + nums[i], i - 1):
return True
used[i] = False
elif nums[i] + cur == target:
used[i] = True
if check(nums, k - 1, 0, len(nums) - 1):
return True
used[i] = False
return False
return check(nums, k, 0, len(nums) - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
if k <= 0:
return False
nums_sum = sum(nums)
if nums_sum % k != 0:
return False
visited = [False] * len(nums)
nums.sort(reverse=True)
return self.canPartition(nums, visited, k, 0, 0, nums_sum / k)
def canPartition(self, nums, visited, k, currentIndex, currentSum, target):
if k == 1:
return True
if currentSum == target:
return self.canPartition(nums, visited, k - 1, 0, 0, target)
for i in range(currentIndex, len(nums)):
if visited[i] is False and currentSum + nums[i] <= target:
visited[i] = True
if self.canPartition(
nums, visited, k, i + 1, currentSum + nums[i], target
):
return True
visited[i] = False
return False | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution(object):
def canPartitionKSubsets(self, nums, k):
he = sum(nums)
if he % k != 0:
return False
target = he // k
visit = [(0) for _ in range(len(nums))]
def dfs(k, ind, cur, cnt):
if k == 0:
return True
if cur == target and cnt > 0:
return dfs(k - 1, 0, 0, 0)
for i in range(ind, len(nums)):
if not visit[i] and cur + nums[i] <= target:
visit[i] = 1
if dfs(k, i + 1, cur + nums[i], cnt + 1):
return True
visit[i] = 0
return False
return dfs(k, 0, 0, 0) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
target, rem = divmod(sum(nums), k)
if rem or max(nums) > target:
return False
n = len(nums)
seen = [0] * n
nums.sort(reverse=True)
def dfs(k, index, current_sum):
if k == 1:
return True
if current_sum == target:
return dfs(k - 1, 0, 0)
for i in range(index, n):
if not seen[i] and current_sum + nums[i] <= target:
seen[i] = 1
if dfs(k, i + 1, current_sum + nums[i]):
return True
seen[i] = 0
return False
return dfs(k, 0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
if sum(nums) % k or len(nums) < k:
return False
if k == 1:
return True
target, used = sum(nums) / k, [(False) for i in range(len(nums))]
def dfs(start, sum_, k):
if k == 1:
return True
if sum_ == target:
return dfs(0, 0, k - 1)
for i in range(start, len(nums)):
if not used[i] and sum_ < target:
used[i] = True
if dfs(i + 1, sum_ + nums[i], k):
return True
used[i] = False
return False
return dfs(0, 0, k) | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
target, rem = divmod(sum(nums), k)
if rem:
return False
def dfs(groups):
if not nums:
return True
v = nums.pop()
for i, group in enumerate(groups):
if group + v <= target:
groups[i] += v
if dfs(groups):
return True
groups[i] -= v
if not group:
break
nums.append(v)
return False
nums.sort()
if nums[-1] > target:
return False
while nums and nums[-1] == target:
k -= 1
nums.pop()
return dfs([0] * k) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP LIST NUMBER VAR |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
total = sum(nums)
if total % k != 0:
return False
target = total // k
nums.sort()
if nums[-1] > target:
return False
while nums and nums[-1] == target:
nums.pop()
k -= 1
return self.helper(nums, target, [0] * k)
def helper(self, nums, target, dp):
if not nums:
return True
num = nums.pop()
for i in range(len(dp)):
if dp[i] + num <= target:
dp[i] += num
if self.helper(nums, target, dp):
return True
dp[i] -= num
if dp[i] == 0:
break
nums.append(num)
return False | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def canPartitionKSubsets(self, nums, k):
total = sum(nums)
if total % k != 0:
return False
subsetTotal = total // k
visited = [0] * len(nums)
return self.helper(k, 0, 0, visited, nums, k, subsetTotal)
def helper(self, remainingSets, index, s, visited, nums, k, subsetTotal):
if remainingSets == 1:
return True
if s == subsetTotal:
return self.helper(remainingSets - 1, 0, 0, visited, nums, k, subsetTotal)
for i in range(index, len(nums)):
if visited[i] == 0 and s + nums[i] <= subsetTotal:
visited[i] = 1
if self.helper(
remainingSets, i + 1, s + nums[i], visited, nums, k, subsetTotal
):
return True
visited[i] = 0
return False | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER |
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000. | class Solution:
def partition(self, nums, target, current, pos, fill):
if fill == 0:
return True
for i in range(pos, len(nums)):
next = nums[:i] + nums[i + 1 :]
if current + nums[i] == target:
if self.partition(next, target, 0, 0, fill - 1):
return True
elif current + nums[i] < target:
if self.partition(next, target, current + nums[i], i, fill):
return True
elif current == 0:
return False
return False
def canPartitionKSubsets(self, nums, k):
s = sum(nums)
if s % k > 0:
return False
target = s // k
return self.partition(nums, target, 0, 0, k) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, A: List[int]) -> int:
visited = collections.Counter(A)
ans = [0]
self.calc(A, visited, [], ans)
return ans[0]
def calc(self, A, visited, tmp, ans):
if len(tmp) == len(A):
ans[0] += 1
for i in list(visited.keys()):
if visited[i] == 0:
continue
visited[i] -= 1
tmp.append(i)
if len(tmp) < 2 or (tmp[-1] + tmp[-2]) ** 0.5 == int(
(tmp[-1] + tmp[-2]) ** 0.5
):
self.calc(A, visited, tmp, ans)
tmp.pop()
visited[i] += 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, nums: List[int]) -> int:
self.ans = 0
def isSquare(v):
x = int(v**0.5)
return x * x == v
def dfs(pos):
if pos >= len(nums):
self.ans += 1
return
used = set()
for i in range(pos, len(nums)):
if nums[i] in used:
continue
used.add(nums[i])
nums[pos], nums[i] = nums[i], nums[pos]
if pos == 0 or pos > 0 and isSquare(nums[pos] + nums[pos - 1]):
dfs(pos + 1)
nums[pos], nums[i] = nums[i], nums[pos]
dfs(0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, A: List[int]) -> int:
n = len(A)
A = sorted(A)
g = [([0] * n) for _ in range(n)]
dp = [([0] * n) for _ in range(1 << n)]
for i in range(n):
for j in range(n):
if i == j:
continue
r = int((A[i] + A[j]) ** 0.5)
if r**2 == A[i] + A[j]:
g[i][j] = 1
for i in range(n):
if i == 0 or A[i] != A[i - 1]:
dp[1 << i][i] = 1
ans = 0
for s in range(1 << n):
for i in range(n):
if not dp[s][i]:
continue
for j in range(n):
if not g[i][j]:
continue
if s & 1 << j:
continue
if j > 0 and not s & 1 << j - 1 and A[j - 1] == A[j]:
continue
dp[s | 1 << j][j] += dp[s][i]
for i in range(n):
ans += dp[(1 << n) - 1][i]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN VAR VAR |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, A: List[int]) -> int:
c = collections.Counter(A)
cand = {i: {j for j in c if int((i + j) ** 0.5) ** 2 == i + j} for i in c}
def dfs(x, left=len(A) - 1):
c[x] -= 1
count = sum(dfs(y, left - 1) for y in cand[x] if c[y]) if left else 1
c[x] += 1
return count
return sum(map(dfs, c)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR FUNC_DEF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, A: List[int]) -> int:
def get_valid():
ans = 0
seen = set()
visited = set()
while q:
cur, remaining = q.popleft()
if not remaining and cur not in seen:
ans += 1
for i, n in enumerate(remaining):
sq = (cur[-1] + n) ** 0.5
if float(int(sq)) == sq:
add = cur + (n,)
remain = remaining[:i] + remaining[i + 1 :]
if (add, remain) not in visited:
q.append((add, remain))
visited.add((add, remain))
return ans
q = deque()
for i, num in enumerate(A):
q.append(((num,), tuple(A[:i] + A[i + 1 :])))
return get_valid() | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.
Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].
Example 1:
Input: [1,17,8]
Output: 2
Explanation:
[1,8,17] and [17,8,1] are the valid permutations.
Example 2:
Input: [2,2,2]
Output: 1
Note:
1 <= A.length <= 12
0 <= A[i] <= 1e9 | class Solution:
def numSquarefulPerms(self, A: List[int]) -> int:
candidates = collections.Counter(A)
graph = {
x: [y for y in candidates if int((x + y) ** 0.5) ** 2 == x + y]
for x in candidates
}
graph[-1] = [x for x in candidates]
def dfs(node, node_left):
if node_left == 0:
return 1
res = 0
for n in graph[node]:
if candidates[n] == 0:
continue
candidates[n] -= 1
res += dfs(n, node_left - 1)
candidates[n] += 1
return res
return dfs(-1, len(A)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.