Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: len_start_number = {} mod_num = 1000000007 len_start_number = {(1,i): 1 for i in range(0,10)} for i in range(2,n+1): len_start_number[(i,1)] = (len_start_number[(i-1,4)] + len_start_number[(i-1,8)]) % mod_num len_start_number[(i,2)] = (len_start_number[(i-1,7)] * 2) % mod_num len_start_number[(i,4)] = (len_start_number[(i-1,1)] + len_start_number[(i-1,7)] + len_start_number[(i-1,0)]) % mod_num len_start_number[(i,5)] = 0 len_start_number[(i,7)] = (len_start_number[(i-1,2)] + len_start_number[(i-1,4)]) % mod_num len_start_number[(i,8)] = (len_start_number[(i-1,1)] * 2) % mod_num len_start_number[(i,0)] = (len_start_number[(i-1,4)] * 2) % mod_num count = 0 for k in [0,1,2,4,5,7,8]: if k in [1,4,7]: count += (2 * len_start_number[(n,k)]) % mod_num else: count += len_start_number[(n,k)] % mod_num return count % mod_num
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
import numpy as np class Solution: def knightDialer(self, n: int) -> int: M = np.matrix([ [0,0,0,0,1,0,1,0,0,0], [0,0,0,0,0,0,1,0,1,0], [0,0,0,0,0,0,0,1,0,1], [0,0,0,0,1,0,0,0,1,0], [1,0,0,1,0,0,0,0,0,1], [0,0,0,0,0,0,0,0,0,0], [1,1,0,0,0,0,0,1,0,0], [0,0,1,0,0,0,1,0,0,0], [0,1,0,1,0,0,0,0,0,0], [0,0,1,0,1,0,0,0,0,0] ]) dp = [1,1,1,1,1,1,1,1,1,1] f = M n = n - 1 while n > 0: if n % 2: dp = dp * M % 1000000007 n = n // 2 M = M * M % 1000000007 return np.sum(dp) % 1000000007 # class Solution: # def knightDialer(self, n: int) -> int: # path = [(4, 6), (6, 8), (7, 9), (4, 8), # (0, 3, 9), (), (0, 1, 7), # (2, 6), (1, 3), (2, 4)] # memo = [] # for i in range(n+1): # memo.append({}) # for j in range(10): # memo[i][j] = -1 # def dp(num, steps): # if steps == 1: # return 1 # if memo[steps][num] != -1: # return memo[steps][num] # ret = 0 # for i in path[num]: # ret += dp(i, steps-1) # memo[steps][num] = ret # return ret % 1000000007 # res = 2 * (dp(1, n) + dp(4, n) + dp(7, n)) + dp(2, n) + dp(5, n) + dp(8, n) + dp(0, n) # res %= 1000000007 # return res
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
#bfs solution #graph problem class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 chessmap = {1:[4,2], 2:[1,3], 3:[4,2], 4:[3,1,0], 0:[4,4]} table = [1]*5 for i in range(2, n+1): tmp = [0] * 5 for j in range(5): for k in chessmap[j]: tmp[j] += table[k] table = tmp return (sum(table)*2-table[0]) % (10**9+7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, l: int) -> int: dpTable = [1] * 10 for _ in range(l - 1): newTable = [0] * 10 for i in [0, 1, 2, 3, 4, 6, 7, 8, 9]: if i == 0: ret = dpTable[4] + dpTable[6] elif i == 1: ret = dpTable[6] + dpTable[8] elif i == 2: ret = dpTable[7] + dpTable[9] elif i == 3: ret = dpTable[4] + dpTable[8] elif i == 4: ret = dpTable[3] + dpTable[9] + dpTable[0] elif i == 6: ret = dpTable[1] + dpTable[7] + dpTable[0] elif i == 7: ret = dpTable[2] + dpTable[6] elif i == 8: ret = dpTable[1] + dpTable[3] elif i == 9: ret = dpTable[2] + dpTable[4] newTable[i] = ret % 1000000007 dpTable = newTable return sum(dpTable) % 1000000007
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: D = 10**9 + 7 l = [1 for _ in range(10)] for _ in range(N-1): l = self.transform_list(l) l = [x % D for x in l] return sum(l) % D def transform_list(self, l: List[int]) -> List[int]: # 0 -> 4, 6 # 1 -> 6, 8 # 2 -> 7, 9 # 3 -> 4, 8 # 4 -> 3, 9, 0 # 5 -> None # 6 -> 1, 7, 0 # 7 -> 2, 6 # 8 -> 1, 3 # 9 -> 2, 4 nl = [0 for _ in range(10)] nl[0] = l[4] + l[6] nl[1] = l[6] + l[8] nl[2] = l[7] + l[9] nl[3] = l[4] + l[8] nl[4] = l[3] + l[9] + l[0] nl[6] = l[1] + l[7] + l[0] nl[7] = l[2] + l[6] nl[8] = l[1] + l[3] nl[9] = l[2] + l[4] return nl
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 0: return n pad = [] for i in range(4): pad.append([1]*3) pad[3][0] = 0 pad[3][2] = 0 for k in range(n-1): newPad = [] for i in range(4): newPad.append([1]*3) newPad[0][0] = (pad[1][2] + pad[2][1])%(10**9 + 7) newPad[0][1] = (pad[2][0] + pad[2][2])%(10**9 + 7) newPad[0][2] = (pad[1][0] + pad[2][1])%(10**9 + 7) newPad[1][0] = (pad[0][2] + pad[2][2] + pad[3][1])%(10**9 + 7) newPad[1][1] = 0 newPad[1][2] = (pad[0][0] + pad[2][0] + pad[3][1])%(10**9 + 7) newPad[2][0] = (pad[1][2] + pad[0][1])%(10**9 + 7) newPad[2][1] = (pad[0][0] + pad[0][2])%(10**9 + 7) newPad[2][2] = (pad[0][1] + pad[1][0])%(10**9 + 7) newPad[3][0] = 0 newPad[3][1] = (pad[1][0] + pad[1][2])%(10**9 + 7) newPad[3][2] = 0 pad = newPad total = 0 for i in range(len(pad)): total += sum(pad[i]) return total%(10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: dp = [[0] * 10 for _ in range(N)] for i in range(N): if i == 0: dp[i] = [1] * 10 continue dp[i][0] = dp[i-1][4] + dp[i-1][6] dp[i][1] = dp[i-1][8] + dp[i-1][6] dp[i][2] = dp[i-1][7] + dp[i-1][9] dp[i][3] = dp[i-1][4] + dp[i-1][8] dp[i][4] = dp[i-1][0] + dp[i-1][3] + dp[i-1][9] dp[i][5] = 0 dp[i][6] = dp[i-1][0] + dp[i-1][1] + dp[i-1][7] dp[i][7] = dp[i-1][2] + dp[i-1][6] dp[i][8] = dp[i-1][1] + dp[i-1][3] dp[i][9] = dp[i-1][2] + dp[i-1][4] #print(dp) return sum(dp[-1]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: dp = [0] * 10 for j in range(10): dp[j] = 1 for i in range(1, N): tmp0 = (dp[4] + dp[6]) % (math.pow(10,9) + 7) tmp1 = (dp[6] + dp[8]) % (math.pow(10,9) + 7) tmp2 = (dp[7] + dp[9]) % (math.pow(10,9) + 7) tmp3 = (dp[4] + dp[8]) % (math.pow(10,9) + 7) tmp4 = (dp[0] + dp[3] + dp[9]) % (math.pow(10,9) + 7) tmp5 = 0 tmp6 = (dp[0] + dp[1] + dp[7]) % (math.pow(10,9) + 7) tmp7 = (dp[2] + dp[6]) % (math.pow(10,9) + 7) tmp8 = (dp[1] + dp[3]) % (math.pow(10,9) + 7) tmp9 = (dp[2] + dp[4]) % (math.pow(10,9) + 7) dp[0] = tmp0 dp[1] = tmp1 dp[2] = tmp2 dp[3] = tmp3 dp[4] = tmp4 dp[5] = tmp5 dp[6] = tmp6 dp[7] = tmp7 dp[8] = tmp8 dp[9] = tmp9 total = 0 for j in range(10): if N > 1 and j == 5: continue total += dp[j] total = int(total % (math.pow(10,9) + 7)) return total
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 0: return n pad = [] for i in range(4): pad.append([1]*3) pad[3][0] = 0 pad[3][2] = 0 for k in range(n-1): newPad = [] for i in range(4): newPad.append([1]*3) newPad[0][0] = pad[1][2] + pad[2][1] newPad[0][1] = pad[2][0] + pad[2][2] newPad[0][2] = pad[1][0] + pad[2][1] newPad[1][0] = pad[0][2] + pad[2][2] + pad[3][1] newPad[1][1] = 0 newPad[1][2] = pad[0][0] + pad[2][0] + pad[3][1] newPad[2][0] = pad[1][2] + pad[0][1] newPad[2][1] = pad[0][0] + pad[0][2] newPad[2][2] = pad[0][1] + pad[1][0] newPad[3][0] = 0 newPad[3][1] = pad[1][0] + pad[1][2] newPad[3][2] = 0 pad = newPad total = 0 for i in range(len(pad)): total += sum(pad[i]) return total%(10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: T = [[0] * (N+1) for i in range(10)] for i in range(10): T[i][1] = 1 for step in range(2, N+1): T[0][step] = T[4][step-1] + T[6][step-1] T[1][step] = T[8][step-1] + T[6][step-1] T[2][step] = T[7][step-1] + T[9][step-1] T[3][step] = T[4][step-1] + T[8][step-1] T[4][step] = T[3][step-1] + T[9][step-1] + T[0][step-1] T[5][step] = 0 T[6][step] = T[1][step-1] + T[7][step-1] + T[0][step-1] T[7][step] = T[6][step-1] + T[2][step-1] T[8][step] = T[1][step-1] + T[3][step-1] T[9][step] = T[2][step-1] + T[4][step-1] return sum([T[i][N] for i in range(10)]) % (pow(10, 9) + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: import numpy as np mod = 10**9 + 7 if n == 1: return 10 M = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 0, 0]]) res = np.matrix([[1]*10]) n -= 1 while n: if n % 2 != 0: res = res * M % mod n -= 1 else: M = M * M % mod n /= 2 return int(np.sum(res)) % mod
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: if n == 1: return 10 dp = [[1,1,1,1,1,0,1,1,1,1] for _ in range(n+1)] for i in range(2, n+1): dp[i][0] = dp[i-1][4] + dp[i-1][6] dp[i][1] = dp[i-1][8] + dp[i-1][6] dp[i][2] = dp[i-1][7] + dp[i-1][9] dp[i][3] = dp[i-1][4] + dp[i-1][8] dp[i][4] = dp[i-1][0] + dp[i-1][9] + dp[i-1][3] dp[i][6] = dp[i-1][1] + dp[i-1][7] + dp[i-1][0] dp[i][7] = dp[i-1][6] + dp[i-1][2] dp[i][8] = dp[i-1][1] + dp[i-1][3] dp[i][9] = dp[i-1][4] + dp[i-1][2] return sum(dp[-1]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: num = 0 stack = [(1, i, i) for i in range(10)] pos = {1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6]} memo = [[1]*10 for _ in range(n)] for i in range(1,n): memo[i][0] = memo[i-1][4] + memo[i-1][6] memo[i][1] = memo[i-1][6] + memo[i-1][8] memo[i][2] = memo[i-1][7] + memo[i-1][9] memo[i][3] = memo[i-1][4] + memo[i-1][8] memo[i][4] = memo[i-1][0] + memo[i-1][3] + memo[i-1][9] memo[i][5] = 0 memo[i][6] = memo[i-1][1] + memo[i-1][7] + memo[i-1][0] memo[i][7] = memo[i-1][2] + memo[i-1][6] memo[i][8] = memo[i-1][1] + memo[i-1][3] memo[i][9] = memo[i-1][2] + memo[i-1][4] return sum(memo[-1]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: # moveable = { # 1: [6, 8], # 2: [7, 9], # 3: [4, 8], # 4: [0, 3, 9], # 5: [], # 6: [0, 1, 7], # 7: [2, 6], # 8: [1, 3], # 9: [2, 4], # 0: [4, 6] # } # MOD = 7 + 1e9 # @lru_cache(maxsize=None) # def helper(step, num=-1): # if step == n: # return 1 # result = 0 # if num == -1: # for i in range(10): # result = (result + helper(step + 1, i)) % MOD # else: # for move in moveable[num]: # result = (result + helper(step + 1, move)) % MOD # return result # return int(helper(0)) moveable = { 1: [6, 8], 2: [7, 9], 3: [4, 8], 4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6], 8: [1, 3], 9: [2, 4], 0: [4, 6] } MOD = 7 + 1e9 dp = [1] * 10 for step in range(n - 1): newDP = [0] * 10 for key in range(10): for move in moveable[key]: newDP[move] = (newDP[move] + dp[key]) % MOD dp = newDP return int(sum(dp) % MOD)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: jump = { 1: [6,8], 2: [7,9], 3: [4,8], 4: [3,9,0], 5: [], 6: [1,7,0], 7: [6,2], 8: [1,3], 9: [2,4], 0: [4,6] } dp = [1]*10 for _ in range(n-1): newDp = [1]*10 for i in range(10): res = 0 for item in jump[i]: res += dp[item] res %= 1000000007 newDp[i] = res dp = newDp return sum(dp)%1000000007
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: a, M = [1]*10, 10**9 + 7 moves = [(4,6), (6,8), (7,9), (4,8), (0,3,9), (), (0,1,7), (2,6), (1,3), (2,4)] for _ in range(1, n): b = [0]*10 for i in range(10): for j in moves[i]: b[i] = (b[i] + a[j]) % M a = b return sum(a)%M
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: dp = [1] * 10 MOD = 10 ** 9 + 7 moves = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [1, 7, 0], [2, 6], [1, 3], [2, 4]] for hop in range(N - 1): new_dp = [0] * 10 for num, count in enumerate(dp): for neigh in moves[num]: new_dp[neigh] = (new_dp[neigh] + count) % MOD dp = new_dp return sum(dp) % MOD
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: # Space O(10) moves = [[4,6],[6,8],[7,9],[4,8],[3,9,0],[],[1,7,0],[2,6],[1,3],[2,4]] dp = [1] * 10 MOD = 10**9 + 7 for i in range(N - 1): next_dp = [0] * 10 for j in range(10): for next_digit in moves[j]: next_dp[j] = (next_dp[j] + dp[next_digit]) % MOD dp = next_dp return sum(dp) % MOD # space O(10 * N) # mapping = {1:[6,8], # 2:[7,9], # 3:[4,8], # 4:[3,9,0], # 5:[], # 6:[1,7,0], # 7:[2,6], # 8:[1,3], # 9:[2,4], # 0:[4,6]} # dp = [[0] * 10 for _ in range(N)] # dp[0] = [1] * 10 # for i in range(1, N): # for j in range(10): # for next_digit in mapping[j]: # dp[i][j] += dp[i - 1][next_digit] # return sum(dp[-1]) % (10**9 + 7)
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, N: int) -> int: moves = [(4,6),(6,8),(7,9),(4,8),(3,9,0),(),(1,7,0),(2,6),(1,3),(2,4)] memo = [1]*10 maxV = (10**9+7) for _ in range(N-1): temp = [0]*10 for i in range(10): for dest in moves[i]: temp[i] += memo[dest] temp[i] %= maxV memo = temp return sum(memo)%maxV
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram: A chess knight can move as indicated in the chess diagram below: We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell). Given an integer n, return how many distinct phone numbers of length n we can dial. You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps. As the answer may be very large, return the answer modulo 109 + 7.   Example 1: Input: n = 1 Output: 10 Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient. Example 2: Input: n = 2 Output: 20 Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94] Example 3: Input: n = 3 Output: 46 Example 4: Input: n = 4 Output: 104 Example 5: Input: n = 3131 Output: 136006598 Explanation: Please take care of the mod.   Constraints: 1 <= n <= 5000
class Solution: def knightDialer(self, n: int) -> int: neighbors = { 0: [4,6], 1: [6,8], 2: [7,9], 3: [4,8], 4: [0,3,9], 5: [], 6: [0,1,7], 7: [2,6], 8: [1,3], 9: [2,4] } # def helper(pos, hops_left): # if hops_left == 1: return 1 # if (pos, hops_left) in cache: # return cache[(pos, hops_left)] # else: # sequences = 0 # for ne in neighbors[pos]: # sequences += helper(ne, hops_left-1) # cache[(pos, hops_left)] = sequences # # print(f\"seq {sequences} for (pos {pos}, hops_left {hops_left})\") # return sequences # cache = {} # counts = [0]*10 # for start in range(10): # counts[start] = helper(start, n) # # print(f\"counts[{start}] = {counts[start]}\") # return sum(counts)%(10**9+7) current_counts = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] for _ in range(n-1): next_counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for src_key in range(10): for dst_key in neighbors[src_key]: next_counts[dst_key] = (next_counts[dst_key] + current_counts[src_key]) % (10**9 + 7) current_counts = next_counts return sum(current_counts) % (10**9 + 7)
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
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 typing import List class Solution: def helper(self, current, arr, index, chars, remaining): if index == len(arr): return current maxLength = current for i in range(index, len(arr), 1): remaining -= len(arr[i]) if current + len(arr[i]) + remaining <= maxLength: continue isGood = True # print(arr[i]) for c in arr[i]: if c in chars: isGood = False break if isGood: # print(arr[i], \"is good\") for c in arr[i]: chars.add(c) maxLength = max(self.helper(current + len(arr[i]), arr, i+1, chars, remaining), maxLength) for c in arr[i]: chars.remove(c) # else: # print(arr[i], \"is bad\") return maxLength def maxLength(self, arr: List[str]) -> int: arr = [a for a in arr if len(a) == len(set(a))] remaining = sum([len(a) for a in arr]) return self.helper(0, arr, 0, set(), remaining) # print(Solution().maxLength([\"un\",\"iq\",\"ue\"]))
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)) #print(uniqELements) return maximum
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): # print(\"Bitlist: {a}, Index: {b}\".format(a=bitlist, b=index)) if index == len(arr): # print(\"returning length\") return len(buildSet(bitlist)) new_bitlist = bitlist[:] new_bitlist[index] = 0 # print(\"recursing with not included\") 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: # print(\"return max of both\") new_bitlist[index] = 1 return max(not_included, recurse(new_bitlist, index+1)) return recurse([0 for _ in arr], 0) # [0, 1, 1, 0]
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())
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) return self.get_max_length(sets, 0, set())
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
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
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, arr, cur): for i, x in enumerate(arr): if cur.intersection(x): continue nxt = cur.union(x) nxt_key = ''.join(sorted(nxt)) if nxt_key not in self.visited: self.visited.add(nxt_key) self.maxlen = max(self.maxlen, len(nxt)) self.recurse(arr[:i] + arr[i+1:], nxt) def maxLength(self, arr: List[str]) -> int: # setify arr = [set(x) for x in arr if len(set(x)) == len(x)] self.maxlen = 0 self.visited = set() self.recurse(arr, set()) return self.maxlen
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): # print(t) arr.append(t) maxVal = max(maxVal, len(val)) # print(dct) return maxVal
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: #pre strip dupes 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 #dfs # incl # not inc incl def recurse(bitlist, index): if index == len(arr): return len(buildSet(bitlist)) new_bitlist = bitlist[:] #index is our depth of the tree 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)
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: #array with all possible unique string combinations so far possible_strings = [''] maximum = 0 for i in range(len(arr)): temp_len = len(possible_strings) for j in range(temp_len): #trying combination x = arr[i] + possible_strings[j] #string is unique if (len(x)==len(set(x))): #append to possible strings possible_strings.append(x) #set max maximum = max(maximum,len(x)) return maximum
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 collections import Counter class Solution: def dfs(self, arr, cur): for i, string_set in enumerate(arr): if cur.intersection(string_set): continue nxt = cur.union(string_set) nxt_key = ''.join(sorted(nxt)) if nxt_key not in self.visited: self.visited.add(nxt_key) self.ans = max(self.ans, len(nxt)) self.dfs(arr[:i] + arr[i:], nxt) def maxLength(self, arr: List[str]) -> int: # N is length of arr, K is longest str item in arr # filter and setify O(NK) time and space new_arr = [] for string in arr: string_set = set(string) if len(string_set) == len(string): new_arr.append(string_set) arr = new_arr # recursive build O(?) with visited O(?) space self.visited = set() self.ans = 0 self.dfs(arr, set()) return self.ans
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
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,'')
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
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 # print(self.visited) 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:])
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: from collections import Counter def maxLength(self, arr: List[str]) -> int: st = [(None,0)] l = 0 ind_dic = {} # ind: dict new_ind = 0 for i in range(len(arr)): ind_dic[i] = Counter(arr[i]) while st: #print(st) sub,ind = st.pop(0) if sub: l = max(l,len(sub)) if ind >= len(arr): continue if not sub: # first word st.append((None,ind + 1)) if len(arr[ind]) == len(set(arr[ind])): st.append((arr[ind],ind+1)) else: cur_dic = Counter(sub) #print(cur_dic.keys()) #print(ind_dic[ind].keys()) #print(set(cur_dic.keys()).intersection(set(ind_dic[ind].keys()))) if set(cur_dic.keys()).intersection(set(ind_dic[ind].keys())): st.append((sub,ind+1)) elif len(arr[ind]) > len(set(arr[ind])): st.append((sub,ind+1)) else: # no common char st.append((sub+arr[ind],ind + 1)) st.append((sub,ind+1)) new_ind = ind return l
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 typing import List class Solution: def uniqueLength(self,string:str)-> int: for i in range(len(string)): if(string.count(string[i]) > 1): return -1 else: return len(string) def maxUnique(self,arr:List[str], index:int, curr_string: str, result: List[int])-> List[int]: # print(uniqueLength(curr_string)) if(index == len(arr)) and (self.uniqueLength(curr_string) > result[0]): result[0] = len(curr_string) return if(index == len(arr)): return self.maxUnique(arr, index+1,curr_string, result) self.maxUnique(arr, index+1, curr_string+ arr[index], result) def maxLength(self,arr:List[str])->int: result = [0]* 1 self.maxUnique(arr, 0, '',result) return result[0]
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: # setify O(N*M) 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
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): return len(set(s)) == len(s)
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.backtrack(set(), arr,0) def backtrack(self,curr, arr, index): if(index >= len(arr)): return len(curr) max_len = 0 for i in range(index, len(arr)): char_set = set(arr[i]) if(curr.intersection(char_set) == set() and len(char_set) == len(arr[i])): curr = curr.union(char_set) max_len = max(max_len,self.backtrack(set(curr), arr, i+1)) curr = curr.difference(char_set) else: max_len = max(max_len,self.backtrack(curr, arr, i+1)) return max_len
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): # start with unique elements in given list dp = [set(x) for x in arr if len(set(x)) == len(x)] for v in arr: # check for duplicates if len(a := set(v)) == len(v): # concat to each valid set in list for b in dp: # skip if sets share letters if a & b: continue # combine sets, add to dp dp.append(a | b) # remove initial sets because we need to concatenate #for x in arr: # if (tmp := set(x)) in dp: # dp.remove(tmp) # make sure we have valid answer return max(len(x) for x in dp) if dp else 0 return get_max_len(arr)
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
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))
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) # print(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
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) # Get filter out bad input. 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
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 a in arr: if len(set(a)) < len(a): continue a = set(a) for c in dp[:]: if a & c: continue dp.append(a | c) return max(len(a) for a in dp) return self.m
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
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
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: # I believe this problem is equivalent to SAT with weighted variables so I # won't try to beat exp complexity. The state space is 2^16 configurations # large so exp complexity shouldn't be a problem. def maxLength(self, arr): # From python docs: https://docs.python.org/3/library/itertools.html#itertools-recipes 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
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
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: # print(string) ans = max(ans, len(string)) return maxLength(i + 1, string) maxLength(i + 1, string + arr[i]) ans = 0 maxLength(0, '') return ans
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_
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)
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.
# https://www.youtube.com/watch?v=N7womGmLXh8 # https://www.youtube.com/watch?v=pD3cHFNyW2I class Solution: # backtracking 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) # use arr[i] dfs(i + 1, s + candi[i]) # without using arr[i] candi = [] for ss in arr: if isUnique(ss) > 0: candi.append(ss) dfs(0, '') return self.maxLen
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.backtrack(set(), arr,0) def backtrack(self,curr, arr, index): if(index >= len(arr)): return len(curr) max_len = 0 for i in range(index, len(arr)): char_set = set([c for c in arr[i]]) if(curr.intersection(char_set) == set() and len(char_set) == len(arr[i])): curr = curr.union(char_set) max_len = max(max_len,self.backtrack(set(curr), arr, i+1)) curr = curr.difference(char_set) else: max_len = max(max_len,self.backtrack(curr, arr, i+1)) return max_len
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) # print(self.output) # print(self.res) 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) # print(st) 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()
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
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]
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.backtrack(set(), arr,0) def backtrack(self,curr, arr, index): if(index >= len(arr)): return len(curr) max_len = 0 for i in range(index, len(arr)): char_set = set([c for c in arr[i]]) if(curr.intersection(char_set) == set() and len(char_set) == len(arr[i])): curr = curr.union(char_set) max_len = max(max_len,self.backtrack(set(curr), arr, i+1)) curr = curr.difference(char_set) else: max_len = max(max_len,self.backtrack(set(curr), arr, i+1)) return max_len
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
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
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.
# https://www.youtube.com/watch?v=N7womGmLXh8 # https://www.youtube.com/watch?v=pD3cHFNyW2I 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(arr) and isUnique(s) > self.maxLen: self.maxLen = len(s) return if i == len(arr): return dfs(i + 1, s) dfs(i + 1, s + arr[i]) dfs(0, '') return self.maxLen
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]
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)
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
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
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) # temp = # for c in arr[idx]: # chars.append(c) helper(idx + 1, chars + list(arr[idx])) helper(0, []) return self.max
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 generateCombinations(arr,i, result, path): if i == len(arr): if path: combination = ''.join(path) if len(set(combination)) == len(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]
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): yield curr for i in range(len(arr)): yield from dfs(arr[i + 1:], res, curr + arr[i]) res = 0 for st in dfs(arr, res, ''): if isUnique(st): res = max(res, len(st)) return res
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(w): return all(v <= 1 for v in collections.Counter(w).values()) def dfs(i, cand): if i < 0: return 0 res = dfs(i - 1, cand) if valid(arr[i]) and all(c in cand for c in arr[i]): new_cand = {c for c in cand if c not in collections.Counter(arr[i]).keys()} res = max(res, dfs(i - 1, new_cand) + len(arr[i])) return res return dfs(len(arr) - 1, {chr(i + ord('a')) for i in range(26)})
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())
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]
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: # backtracking function def helper(arr, substring, i): # get the length of the substring without char duplication unique_length = len(set(substring)) # if there is no duplicate chars if len(substring) == unique_length: self.max_len = max(self.max_len, unique_length) # check if we reached the last substring in arr 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
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: # I believe this problem is equivalent to SAT with weighted variables so I # won't try to beat exp complexity. The state space is 2^16 configurations # large so exp complexity shouldn't be a problem. 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
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
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
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)
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])
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): # print(self.tmp,self.len) if pos == len(self.arr): if self.len > self.mlen: # print(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() self.tmp = self.tmp[:-l]
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))
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): # print(self.tmp,self.len) if pos == len(self.arr): if self.len > self.mlen: # print(self.len,self.mlen) self.mlen = self.len return 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() self.tmp = self.tmp[:-l] self.backtrack(pos+1)
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): # print(self.tmp,self.len) if pos == len(self.arr): if self.len > self.mlen: # print(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() # self.tmp = self.tmp[:-l]
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) # store the intermediate sets of unique characters charSets = [set()] # iterate over each string for s in arr: # iterate over each char set to see if we can add the chars in s into it currSet = set(s) # skip string swith duplicate chars if len(currSet) != len(s): continue currSize = len(charSets) for idx in range(currSize): charSet = charSets[idx] # check if the sets have an intersection (duplicate chars) if charSet & currSet: continue charSets.append(charSet | currSet) return max(len(charSet) for charSet in charSets)
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
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: # https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/discuss/631245/Python-3-Easy-Code-Intutive-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
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: maxlen = 0 unique = [''] def isvalid(s): return len(s) == len(set(s)) for word in arr: for j in unique: tmp = word + j if isvalid(tmp): unique.append(tmp) maxlen = max(maxlen, len(tmp)) return maxlen
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: # we can concat new_path = new_chars | path backtrack(i+1, new_path) backtrack(0, set()) return self.res
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 = [w for w in arr if len(w)==len(set(w))] hm = {} res = 0 dp = {0:0} for i, w in enumerate(arr): bitmask = 0 for c in set(w): bit_set = 1<<(ord(c)-ord('a')) bitmask ^= bit_set hm[i] = bitmask for i in range(len(arr)): for p in list(dp.keys()): if not hm[i]&p: now = hm[i]^p dp[now] = max(dp.get(now, 0), dp[p]+len(arr[i])) return max(dp.values())
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, l: List[str]) -> int: selected = set() def ok(word, s) -> bool: return not any(v in s for v in word) and len(word) == len(set(word)) def backTrack(l: List[str], curr_index: int, selected: set) -> int: maxLength = 0 if curr_index != -1: for v in l[curr_index]: # add the chosen word to selected selected.add(v) for i in range(curr_index+1,len(l)): if ok(l[i],selected): # word is good - so we choose it maxLength = max(maxLength, len(l[i]) + backTrack(l, i, selected.copy())) return maxLength return backTrack(l, -1, selected)
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(start, seen): nonlocal res if start==len(arr): res = max(res, sum(len(arr[i]) for i in seen) ) else: is_end = True for i in graph[start]: if all(len(arr[j].intersection(arr[i]))==0 for j in seen): is_end = False seen.append(i) dfs(i, seen) seen.pop() if is_end: res = max(res, sum(len(arr[i]) for i in seen) ) arr = [item for item in arr if len(item)==len(set(item))] arr = list(map(set, arr)) graph = collections.defaultdict(list) for i in range(len(arr)): for j in range(i+1, len(arr)): if len(arr[i].intersection(arr[j]))==0: graph[i].append(j) print((dict(graph))) res = 0 for i in range(len(arr)): dfs(i, [i]) return res
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.
#https://wentao-shao.gitbook.io/leetcode/graph-search/1239.maximum-length-of-a-concatenated-string-with-unique-characters class Solution: def maxLength1(self, arr: List[str]) -> int: def dfs(start, seen): nonlocal res if start==len(arr): res = max(res, sum(len(arr[i]) for i in seen) ) else: is_end = True for i in graph[start]: if all(len(arr[j].intersection(arr[i]))==0 for j in seen): is_end = False seen.append(i) dfs(i, seen) seen.pop() if is_end: res = max(res, sum(len(arr[i]) for i in seen) ) arr = [item for item in arr if len(item)==len(set(item))] arr = list(map(set, arr)) graph = collections.defaultdict(list) for i in range(len(arr)): for j in range(i+1, len(arr)): if len(arr[i].intersection(arr[j]))==0: graph[i].append(j) res = 0 for i in range(len(arr)): dfs(i, [i]) return res def maxLength(self, arr: List[str]) -> int: def dfs(seen): nonlocal res is_end = True for i in graph[seen[-1]]: if all(len(arr[j].intersection(arr[i]))==0 for j in seen): is_end = False seen.append(i) dfs(seen) seen.pop() if is_end: res = max(res, sum(len(arr[i]) for i in seen) ) arr = [item for item in arr if len(item)==len(set(item))] arr = list(map(set, arr)) graph = collections.defaultdict(list) for i in range(len(arr)): for j in range(i+1, len(arr)): if len(arr[i].intersection(arr[j]))==0: graph[i].append(j) res = 0 for i in range(len(arr)): dfs([i]) return res
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)]) # word, index to start search from 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))
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): # Base case if index >= len(self.array): return len(prefix) # Recursive cases if include: # Do not include a string that has duplicate characters within itself if len(self.array[index]) != len(set(self.array[index])): return len(prefix) # Do not include a string that contains letters already in 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), )
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: # print(piece) next_uniques = set() for unique in uniques: concat = piece + unique # print(\"{0}: {1}\".format(concat, self.is_unique(concat))) 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
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 collections import Counter class Solution: def maxLength(self, arr) -> int: if(len(arr) ==1): return len(arr[0]) self.max_val = 0 max_val = float('-inf') def check(string): c = Counter(string) for key, val in c.items(): if(c[key]>1): return False return True def _helper(string, index): if check(string): self.max_val = max(self.max_val, len(string)) else: return if index >= len(arr): return for i in range(index, len(arr)): newString = string + arr[i] _helper(newString, i + 1) print(_helper('', 0)) return self.max_val
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 max_length = 0 duplicates = set([]) from collections import deque queue = deque([]) for idx, val in enumerate(arr): if val in duplicates or self.doesDuplicateExist(val): continue queue.appendleft((idx, val, len(val))) duplicates.add(val) n = len(arr) while len(queue) != 0: idx, curr, curr_len = queue.pop() max_length = max(max_length, curr_len) for i in range(idx+1, n): newWord = curr + arr[i] if newWord in duplicates or self.doesDuplicateExist(newWord): continue queue.appendleft((i, newWord, len(newWord))) duplicates.add(newWord) return max_length def doesDuplicateExist(self, val): dup = set() for char in val: if char in dup: return True dup.add(char) return False
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): # Base case if index >= len(self.array): return len(prefix) # Recursive case if include and len(self.array[index]) == len(set(self.array[index])): 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), )
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