description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, arr, n, m, x):
r = n - 1
c = 0
while r > -1 and c < m:
ele = arr[r][c]
if ele == x:
return True
elif x > ele:
c += 1
elif x < ele:
r -= 1
return False | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER |
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, matrix, n, m, x):
def binary_search(arr, key):
l = 0
r = len(arr) - 1
while l <= r:
mid = (l + r) // 2
if arr[mid] == key:
return 1
elif arr[mid] > key:
r = mid - 1
else:
l = mid + 1
return 0
ans = False
for i in range(n):
if matrix[i][0] <= x and matrix[i][-1] >= x:
ans = ans or binary_search(matrix[i], x)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, matrix, n, m, x):
l = []
for i in range(n):
l += matrix[i]
try:
v = l.index(x)
i = 1
except ValueError:
i = 0
return i | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR |
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, matrix, n, m, x):
t = []
r = []
for i in range(len(matrix)):
t = matrix[i]
for j in range(len(t)):
r.append(t[j])
if x in r:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, a, n, m, x):
k, l = 0, m - 1
while k < n - 1 and l >= 0:
if a[k][l] > x:
l -= 1
elif a[k][l] < x:
k += 1
else:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a matrix of size n x m, where every row and column is sorted in increasing order, and a number x. Find whether element x is present in the matrix or not.
Example 1:
Input:
n = 3, m = 3, x = 62
matrix[][] = {{ 3, 30, 38},
{36, 43, 60},
{40, 51, 69}}
Output: 0
Explanation:
62 is not present in the matrix,
so output is 0.
Example 2:
Input:
n = 1, m = 6, x = 55
matrix[][] = {{18, 21, 27, 38, 55, 67}}
Output: 1
Explanation: 55 is present in the matrix.
Your Task:
You don't need to read input or print anything. Complete the function search() that takes n, m, x, and matrix[][] as input parameters and returns a boolean value. True if x is present in the matrix and false if it is not present.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, M <= 1000
1 <= mat[][] <= 10^{5}
1 <= X <= 1000 | class Solution:
def search(self, matrix, r, c, x):
l, d = c - 1, 0
while l >= 0 and d <= r - 1:
if matrix[l][d] == x:
return 1
elif matrix[l][d] < x:
d += 1
else:
l -= 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
possibilities = set()
def swap(arr, l, r):
if l == r:
return
arr[l], arr[r] = arr[r], arr[l]
def add_permutation(arr):
nonlocal possibilities
for i in range(len(arr)):
possibilities.add("".join(arr[: i + 1]))
def build_permutations(arr, start=0):
if start >= len(arr):
add_permutation(arr)
return
for i in range(start, len(arr)):
swap(arr, i, start)
build_permutations(arr, start + 1)
swap(arr, i, start)
build_permutations(list(tiles))
return len(possibilities) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def recur(tiles):
if not tiles:
return set()
ans = set()
for i, v in enumerate(tiles):
ans.add(v)
new = recur(tiles[:i] + tiles[i + 1 :])
ans.update(new)
for k in new:
ans.add(v + k)
return ans
ans = recur(tiles)
return len(ans) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def freq(tiles):
d = {}
for c in tiles:
if c in d:
d[c] += 1
else:
d[c] = 1
return d
def build(d, s):
new_d = {}
for key, value in d.items():
if key == s:
new_d[key] = value - 1
else:
new_d[key] = value
return new_d
def generate(options):
sol = []
for key, value in options.items():
if value > 0:
sol.append(key)
fringe = generate(build(options, key))
sol += fringe
sol += [(key + f) for f in fringe]
return sol
return len(set(generate(freq(tiles)))) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
result = set()
def dfs_helper(path, t):
if path:
result.add(path)
for i in range(len(t)):
dfs_helper(path + t[i], t[:i] + t[i + 1 :])
dfs_helper("", tiles)
return len(result) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
setter = set()
res = []
for r in range(1, len(tiles) + 1):
res.append(list(itertools.permutations(tiles, r)))
result = []
for re in res:
result.append(list(set(re)))
leng = 0
for i in result:
leng += len(i)
return leng | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def dfs(state):
res.append(state.copy())
for i in range(len(state)):
if state[i] < full_state[i]:
state[i] += 1
dfs(state)
state[i] -= 1
memo = collections.Counter(tiles)
full_state = list(memo.values())
state = [(0) for _ in range(len(memo))]
res = []
dfs(state)
return len(res) - 1 | CLASS_DEF FUNC_DEF VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def __init__(self):
self.seen = set()
self.result = set()
def numTilePossibilities(self, tiles: str) -> int:
self.dfs(tiles, 0, [])
return len(self.result) - 1
def dfs(self, string, idx, path):
st = "".join(path)
if st not in self.seen:
self.result.update("".join(p) for p in permutations(st))
self.seen.add(st)
for i in range(idx, len(string)):
self.dfs(string, i + 1, path + [string[i]]) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR NUMBER LIST RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
d = {}
for i in range(len(tiles)):
if tiles[i] in d:
d[tiles[i]] += 1
else:
d[tiles[i]] = 1
def countnum(d):
if d == {}:
return 0
c = 0
s = set(d.items())
for k, v in s:
d[k] -= 1
if d[k] == 0:
del d[k]
c += 1 + countnum(d)
if k in d:
d[k] += 1
else:
d[k] = 1
return c
return countnum(d) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR DICT RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
@lru_cache(None)
def find(s):
if len(s) <= 1:
return set([s])
ret = set()
for i in range(len(s)):
head = s[i]
tails = find(s[:i] + s[i + 1 :])
ret = ret.union(tails)
for tail in tails:
for j in range(len(tail) + 1):
temp = tail[:j] + head + tail[j:]
ret.add(temp)
return ret
res = find(tiles)
return len(set(res)) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
result = set()
vis = [0] * len(tiles)
def dfs(res, depth):
if res:
result.add(res)
if depth == len(tiles) - 1:
result.add(res)
for i in range(len(tiles)):
if not vis[i]:
vis[i] = 1
dfs(res + tiles[i], depth + 1)
vis[i] = 0
dfs("", -1)
return len(result) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def __init__(self):
self.total = 0
self.string = []
self.dict_ = {}
self.arr = []
self.n = 0
def perm(self):
if self.n == len(self.string):
self.total += 1
for i in range(len(self.arr)):
if self.arr[i][1] > 0:
self.arr[i][1] -= 1
self.string.append(self.arr[i][0])
self.perm()
self.string.pop()
self.arr[i][1] += 1
def numTilePossibilities(self, tiles: str) -> int:
for string in tiles:
if string in self.dict_:
self.dict_[string] += 1
else:
self.dict_[string] = 1
for key in self.dict_:
temp = [key, self.dict_[key]]
self.arr.append(temp)
for i in range(1, len(tiles) + 1):
self.n = i
self.perm()
return self.total | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
output = set()
def helper(result, options):
if not options:
return
for idx, o in enumerate(options):
tmp = options[:]
tmp.pop(idx)
output.add("".join(result + [o]))
helper(result + [o], tmp)
helper([], list(tiles))
return len(output) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | def possible(tiles):
variants = set()
if len(tiles) == 1:
variants.add(tiles[0])
else:
for i in range(len(tiles)):
t = possible(tiles[:i] + tiles[i + 1 :])
variants.update(t)
for j in t:
variants.add(tiles[i] + j)
return variants
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
return len(possible(tiles)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
result = set()
maxx = len(tiles)
def dfs(result, tiles, current_sequence=""):
result.add(current_sequence)
for t in tiles:
tiles_c = tiles.replace(t, "", 1)
dfs(result, tiles_c, current_sequence + t)
dfs(result, tiles)
return len(result) - 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
memos = dict()
def numTilePossibilities(self, tiles: str) -> int:
tiles = "".join(sorted(tiles))
uniques = self.step(tiles)
return len(uniques) - 1
def step(self, tiles: str) -> set:
if len(tiles) == 0:
return {""}
if tiles not in self.memos:
uniques = set()
for i in range(len(tiles)):
c = tiles[i]
substr = tiles[:i] + tiles[i + 1 :]
substrs_set = self.step(substr)
for substr in substrs_set:
uniques.add(substr)
for j in range(len(substr) + 1):
new_str = substr[:j] + c + substr[j:]
uniques.add(new_str)
self.memos[tiles] = uniques
return self.memos[tiles] | CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
count = {}
for i in tiles:
if i not in count:
count[i] = 0
count[i] += 1
op = set()
for ch in count:
tempString = ch
op.add(tempString)
count[ch] -= 1
tempCount = count.copy()
buildString(tempString, tempCount, op)
count[ch] += 1
return len(op)
def buildString(currString, count, op):
flag = True
for i in count:
if count[i] != 0:
flag = False
break
if flag:
return
for ch in count:
if count[ch] == 0:
continue
tempString = currString
tempString += ch
op.add(tempString)
count[ch] -= 1
tempCount = count.copy()
buildString(tempString, tempCount, op)
count[ch] += 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def backtrack(i, c):
if i == n:
return
for k in c:
if c[k] > 0:
self.ans += 1
backtrack(i + 1, c - Counter(k))
n = len(tiles)
counter = Counter(tiles)
self.ans = 0
backtrack(0, counter)
return self.ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR RETURN FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
d = set()
for i in range(len(tiles)):
for x in list(permutations(tiles, i + 1)):
d.add("".join(x))
print(d)
return len(d) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def subsets(self, tiles, buff, buff_index, boolean, s):
if tuple(buff[:buff_index]) not in s:
s.add(tuple(buff[:buff_index]))
self.count += 1
if len(buff) == buff_index:
return
for i in range(0, len(tiles)):
if not boolean[i]:
buff[buff_index] = tiles[i]
boolean[i] = True
self.subsets(tiles, buff, buff_index + 1, boolean, s)
boolean[i] = False
def numTilePossibilities(self, tiles: str) -> int:
self.count = 0
buff = [None] * len(tiles)
boolean = [False] * len(tiles)
s = set()
self.subsets(tiles, buff, 0, boolean, s)
return self.count - 1 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def perm(k, a):
if k == 0:
return []
elif k == 1:
return [x for x in a]
else:
return [
(a[i] + y)
for i in range(len(a))
for y in perm(k - 1, a[:i] + a[i + 1 :])
]
out = set()
for i in range(1, len(tiles) + 1):
for x in perm(i, tiles):
out.add(x)
return len(out) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def next_permutation(a):
for i in range(len(a) - 1, 0, -1):
if a[i] > a[i - 1]:
ps = i
for j in range(i + 1, len(a)):
if a[j] > a[i - 1] and a[ps] > a[j]:
ps = j
a[ps], a[i - 1] = a[i - 1], a[ps]
p1 = i
p2 = len(a) - 1
while p1 < p2:
a[p1], a[p2] = a[p2], a[p1]
p1 += 1
p2 -= 1
return True
return False
n = 1
for i in range(1, len(tiles) + 1):
n *= i
a = set()
perm = []
for i in range(len(tiles)):
perm.append(i)
for _ in range(n):
cur = ""
for i in range(len(tiles)):
cur += tiles[perm[i]]
for i in range(len(tiles)):
a.add(cur[: i + 1])
next_permutation(perm)
return len(a) | CLASS_DEF FUNC_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
words = set()
curr = ""
tile_count = {}
for x in tiles:
if x not in tile_count:
tile_count[x] = 0
tile_count[x] += 1
def walk(curr, tiles, words):
for tile in tiles:
if tiles[tile] > 0:
temp = curr + tile
temp_tiles = tiles.copy()
temp_tiles[tile] -= 1
words.add(temp)
walk(temp, temp_tiles, words)
for tile in tile_count:
walk("", tile_count, words)
return len(words) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def __init__(self):
self.res = set()
def backtrack(self, tiles, curr, indices):
for i in range(len(tiles)):
if i not in set(indices):
curr += tiles[i]
indices.append(i)
self.res.add(curr)
if len(curr) < len(tiles):
self.backtrack(tiles, curr, indices)
curr = curr[:-1]
indices.pop()
def numTilePossibilities(self, tiles: str) -> int:
self.backtrack(tiles, "", [])
return len(self.res) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR STRING LIST RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, s: str) -> int:
ans, C = 0, Counter(s)
def dfs(C, cur):
nonlocal ans
if cur:
ans += 1
if not C:
return
C1 = C.copy()
for x in C:
cur.append(x)
C1[x] -= 1
if C1[x] == 0:
del C1[x]
dfs(C1, cur)
cur.pop()
C1[x] += 1
dfs(C, cur=[])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER IF VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
combination_set = self.generateTilePossibilities(tiles)
return len(combination_set)
def generateTilePossibilities(self, tiles: str):
n = len(tiles)
combination_set = set()
for index in range(n):
combination_set.add(tiles[index])
if n > 1:
for index in range(n):
tiles_without_n = tiles[0:index] + tiles[index + 1 :]
additional_combinations = self.generateTilePossibilities(
tiles_without_n
)
combination_set.add(tiles_without_n)
for combination in additional_combinations:
combination_set.add(combination)
for second_index in range(len(combination)):
new_tile_combination = (
combination[0:second_index]
+ tiles[index]
+ combination[second_index:]
)
combination_set.add(new_tile_combination)
return combination_set | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | from itertools import permutations
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
total = 0
for size in range(1, len(tiles) + 1):
total += len(set(permutations(tiles, size)))
return total | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def get_permute(self, opts, cur_sol):
for i in range(len(opts)):
c = opts[i]
cur_sol.append(c)
self.solutions.add("".join(cur_sol))
opts2 = opts
opts2 = opts2[:i] + opts2[i + 1 :]
self.get_permute(opts2, cur_sol)
cur_sol.pop()
def numTilePossibilities(self, tiles: str) -> int:
self.solutions = set()
self.get_permute(tiles, [])
return len(self.solutions) | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
pos = set()
def choose(s, n, pref=""):
if n == 0:
pos.add(pref)
for i in range(len(s)):
choose(s[:i] + s[i + 1 :], n - 1, pref + s[i])
for i in range(1, len(tiles) + 1):
choose(tiles, i)
return len(pos) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
res = set()
def seq(s, l):
if len(l) == 0:
res.add(s)
return
seq(s, l[1:])
for i in range(len(l)):
seq(s + l[i], l[:i] + l[i + 1 :])
seq("", list(tiles))
return len(res) - 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
res = 0
freqs = [(f + 1) for f in Counter(tiles).values()]
for t in itertools.product(*map(range, freqs)):
n = sum(t)
subtotal = math.factorial(n)
for freq in t:
subtotal //= math.factorial(freq)
res += subtotal
return res - 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def dfs(elements):
if prev_elements:
result.add("".join(prev_elements))
for e in elements:
next_elements = elements[:]
next_elements.remove(e)
prev_elements.append(e)
dfs(next_elements)
prev_elements.pop()
result = set()
prev_elements = []
dfs(list(tiles))
return len(result) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def counts(self, collect):
answer = 1
for char in collect:
if collect[char]:
answer += self.counts(collect - collections.Counter(char))
return answer
def numTilePossibilities(self, c):
return self.counts(collections.Counter(c)) - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def backtracking(idx=0, seq="", remaining=tiles):
for i, tile in enumerate(remaining):
res.add(seq + tile)
backtracking(idx + 1, seq + tile, remaining[:i] + remaining[i + 1 :])
res = set()
backtracking()
return len(res) | CLASS_DEF FUNC_DEF VAR FUNC_DEF NUMBER STRING VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
result = 0
def numTilePossibilities(self, tiles: str) -> int:
counter = Counter(tiles)
self.dfs(counter, [])
return self.result
def dfs(self, counter, curr):
if curr:
self.result += 1
for x in counter:
curr1 = curr.copy()
counter1 = counter.copy()
curr1.append(x)
counter1[x] -= 1
if counter1[x] == 0:
del counter1[x]
self.dfs(counter1, curr1) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST RETURN VAR VAR FUNC_DEF IF VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def recur(tiles):
if not tiles:
return set()
ans = set()
temp = ""
while tiles:
v = tiles[0]
ans.add(v)
new = recur(temp + tiles[1:])
ans.update(new)
for k in new:
ans.add(v + k)
temp += tiles[0]
tiles = tiles[1:]
return ans
ans = recur(tiles)
return len(ans) | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def count(tiles):
counter = set()
if len(tiles) == 1:
counter.add(tiles)
elif len(tiles) == 2:
counter.add(tiles)
counter.add(tiles[::-1])
counter.add(tiles[0])
counter.add(tiles[1])
else:
for idx, i in enumerate(tiles):
x = count(tiles[:idx] + tiles[idx + 1 :])
extra = set()
for j in x:
extra.add(tiles[idx] + j)
extra.add(j + tiles[idx])
for k in range(1, len(j) - 1):
extra.add(j[:k] + tiles[idx] + j[k + 1 :])
x.update(extra)
counter.update(x)
return counter
return len(count(tiles)) | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
return len(
set(
x
for i in range(1, len(tiles) + 1)
for x in itertools.permutations(tiles, i)
)
) | CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
ans = 0
s = set()
def tot(curr, rem):
nonlocal s
if curr[-1]:
s.add(tuple(curr[-1]))
for i in range(len(rem)):
el = curr[-1]
if rem[i] == el:
continue
tot(curr + [el + [rem[i]]], rem[:i] + rem[i + 1 :])
tot([[]], tiles)
return len(s) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST LIST VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
tiles = "".join(sorted(tiles))
memo = {}
def helper(s, k):
if (s, k) not in memo:
if k == 0:
memo[s, k] = 1
else:
last, ans = "", 0
for i in range(len(s)):
if s[i] != last:
last = s[i]
ans += helper(s[:i] + s[i + 1 :], k - 1)
memo[s, k] = ans
return memo[s, k]
ret = 0
for k in range(1, len(tiles) + 1):
ret += helper(tiles, k)
return ret | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def helper(curr, tiles):
for i in range(len(tiles)):
curr.append(tiles[i])
pos.add(str(curr))
c = tiles.copy()
c.pop(i)
helper(curr.copy(), c)
curr.pop()
tiles = list(tiles)
pos = set()
helper([], tiles)
return len(pos) | CLASS_DEF FUNC_DEF VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def poss(self, tiles: str) -> set:
if len(tiles) == 1:
return set([tiles[0]])
output = set()
for i in range(len(tiles)):
elem = tiles[i]
res = self.poss(tiles[:i] + tiles[i + 1 :])
output = output.union(res)
for elem in res:
output.add(tiles[i] + elem)
return output
def numTilePossibilities(self, tiles: str) -> int:
if len(tiles) == 0:
return 0
return len(self.poss(tiles)) | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
if len(tiles) < 2:
return len(tiles)
nodes = []
for letter in tiles:
nodes.append(Node(letter))
for node in nodes:
for other_node in nodes:
if node != other_node:
node.edges.append(other_node)
paths = set()
seen = set()
for node in nodes:
self.backtrack(node, seen, node.char, paths)
print(paths)
return len(paths)
def backtrack(self, node, seen: set, path: str, paths: set):
if node in seen:
return
path += node.char
if path and path not in paths:
paths.add(path)
for neighbor in node.edges:
seen.add(node)
self.backtrack(neighbor, seen, path, paths)
seen.remove(node)
class Node:
def __init__(self, char: str):
self.char = char
self.edges = [] | CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR LIST |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
result = set()
def backtrack(left_tiles, curr_seq):
if len(curr_seq) == k:
result.add("".join(curr_seq))
return
else:
for i in range(len(left_tiles)):
curr_seq.append(left_tiles[i])
backtrack(left_tiles[:i] + left_tiles[i + 1 :], curr_seq)
curr_seq.pop()
for k in range(1, len(tiles) + 1):
backtrack(tiles, [])
return len(result) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def make_tile(self, curr, used, letters, size):
if len(curr) == size:
self.res += 1
return
i = 0
while i < len(letters):
if i not in used:
used.add(i)
curr.append(letters[i])
self.make_tile(curr, used, letters, size)
curr.pop(-1)
used.remove(i)
while i + 1 < len(letters) and letters[i] == letters[i + 1]:
i += 1
i += 1
def numTilePossibilities(self, tiles: str) -> int:
self.res = 0
letters = sorted(tiles)
for size in range(1, len(tiles) + 1):
self.make_tile([], set(), letters, size)
return self.res | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR RETURN VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def solve(s, visited):
seen.add(s)
if len(s) == len(tiles):
return 0
for i, v in enumerate(tiles):
if i not in visited:
solve(s + v, visited | {i})
return len(seen)
seen = set()
return solve("", set()) - 1 | CLASS_DEF FUNC_DEF VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR NUMBER VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
seen = set()
ans = set()
def backtrack(tiles, seen, curr):
if curr != "" and curr not in ans:
ans.add(curr)
for i in range(len(tiles)):
if i not in seen:
seen.add(i)
backtrack(tiles, seen, curr + tiles[i])
seen.remove(i)
backtrack(tiles, seen, "")
return len(ans) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR |
You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
Example 1:
Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: tiles = "AAABBC"
Output: 188
Example 3:
Input: tiles = "V"
Output: 1
Constraints:
1 <= tiles.length <= 7
tiles consists of uppercase English letters. | class Solution:
def numTilePossibilities(self, tiles: str) -> int:
tile_counts = collections.Counter(tiles)
len_counts = [1] + [0] * len(tiles)
for tile in tile_counts:
new_len_counts = [0] * (len(tiles) + 1)
num_tiles = tile_counts[tile]
for num_inserted in range(num_tiles + 1):
for old_len, old_len_count in enumerate(len_counts):
new_len = old_len + num_inserted
if new_len > len(tiles):
break
num_patterns = math.comb(new_len, num_inserted)
new_len_counts[new_len] += old_len_count * num_patterns
len_counts = new_len_counts
return sum(len_counts) - 1 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
l = list(map(int, input().split()))
q = int(input())
d = {}
for i in range(n):
item = l[i]
for j in range(i, n):
item = min(item, l[j])
if item in d:
d[item] += 1
else:
d[item] = 1
for i in range(q):
k = int(input())
if k in d.keys():
print(d[k])
else:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
l = list(map(int, input().split()))
q = int(input())
for _ in range(q):
cnt = 0
k = int(input())
for i in range(n):
for j in range(i, n):
if min(l[i : j + 1]) == k:
cnt += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
d[a[i]] = 0
for i in range(n):
for j in range(i, n):
l = a[i : j + 1]
d[min(l)] += 1
q = int(input())
for _ in range(q):
x = int(input())
if x in d:
print(d[x])
else:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
arr = [int(i) for i in input().split()]
p = sorted(arr)
mn = p[0]
mx = p[-1]
for i in range(int(input())):
k = int(input())
if k < mn or k > mx:
print(0)
else:
ans = 0
for i in range(n):
c_r = 0
c_l = 0
if arr[i] == k:
L = arr[:i]
R = arr[i + 1 :]
L = L[::-1]
for j in L:
if arr[i] > j:
break
else:
c_l += 1
for j in R:
if j > arr[i]:
c_r += 1
else:
break
ans += c_l * (c_r + 1) + c_r + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
A = list(map(int, input().strip().split()))
D = dict()
for l in range(len(A)):
for r in range(l + 1, len(A) + 1):
v = min(A[l:r])
D[v] = 1 if not v in D else D[v] + 1
q = int(input())
for i in range(q):
v = int(input())
print(0 if not v in D else D[v]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | array_size = int(input())
arr = input().split(" ")
arr = list(map(int, arr))
query_no = int(input())
for tc in range(query_no):
k = int(input())
count = 0
result = [[]]
for i in range(len(arr) + 1):
for j in range(i + 1, len(arr) + 1):
sub = arr[i:j]
result.append(sub)
for i in result:
if i != []:
if k == min(i):
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR LIST IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | N = int(input())
A = [int(x) for x in input().split()]
for _ in range(int(input())):
K = int(input())
total = 0
for i in range(N):
a = 0
count = 0
forward = 0
backward = 1
if A[i] == K:
if i != A.index(K):
a = 1
for j in range(i, N):
if A[j] >= K:
forward += 1
else:
break
for j in range(i - 1, -1, -1):
if a == 1:
if A[j] > K:
backward += 1
else:
break
elif a == 0:
if A[j] >= K:
backward += 1
else:
break
count += forward * backward
total += count
print(total) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
a = [int(word) for word in input().split()]
q = int(input())
for iq in range(q):
k = int(input())
ways = 0
last_lt = -1
last_leq = -1
for i in range(n):
if a[i] < k:
last_lt = i
if a[i] <= k:
last_leq = i
ways += last_leq - last_lt
print(ways) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | def solve(a, k):
listOfMin = []
length = len(a)
for i in range(length):
for j in range(i + 1, length + 1):
listOfMin.append(min(a[i:j]))
count = 0
for e in listOfMin:
if e == k:
count += 1
return count
sizeOfArray = int(input())
a = []
elements = input().split(" ")
for ele in elements:
a.append(int(ele))
noOfQueries = int(input())
for i in range(noOfQueries):
k = int(input())
print(solve(a, k)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
a = []
x = l[i]
for j in range(i, n):
a.append(l[j])
x = min(x, l[j])
if x in d:
d[x] += 1
else:
d[x] = 1
q = int(input())
while q > 0:
b = int(input())
if b in d:
print(d[b])
else:
print(0)
q -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | input()
arr = list(map(int, input().split()))
Q = int(input())
for item in range(Q):
K = int(input())
total = 0
for i in range(len(arr)):
hasK = False
if arr[i] >= K:
if arr[i] == K:
hasK = True
total += 1
j = i + 1
while j < len(arr) and arr[j] >= K:
if arr[j] == K:
hasK = True
if hasK:
total += 1
j += 1
print(total) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | t = 1
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
k = l[i]
for j in range(i, n):
k = min(k, l[j])
try:
d[k] += 1
except:
d[k] = 1
q = int(input())
for _ in range(q):
k = int(input())
try:
print(d[k])
except:
print(0)
t -= 1 | ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | import itertools
import sys
def solve(N, A, K):
count_sets = lambda g, i: (i + 1) * (len(g) - i)
groups = [list(g) for k, g in itertools.groupby(A, lambda x: x >= K) if k]
total = 0
for g in groups:
if g.count(K) > 0:
idx = [i for i, a in enumerate(g) if a == K]
for i in range(len(idx)):
if i == 0:
total += (idx[i] + 1) * (len(g) - idx[i])
else:
total += (idx[i] - idx[i - 1]) * (len(g) - idx[i])
return total
def solve_brute(N, A, K):
return sum(1 for i in range(N) for j in range(i, N) if min(A[i : j + 1]) == K)
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
for _ in range(Q):
K = int(sys.stdin.readline())
print(solve_brute(N, A, K)) | IMPORT IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
arr = list(map(int, input().split()))
q = int(input())
for i in range(q):
num = int(input())
ans = 0
for j in range(n):
if arr[j] == num:
temp = 1
count = 1
for k in range(j + 1, n):
if min(num, arr[k]) == num:
count += 1
else:
break
temp *= count
count = 1
for k in range(j - 1, -1, -1):
if min(num, arr[k]) == num:
if num == arr[k]:
break
count += 1
else:
break
temp *= count
ans += temp
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | def sub(arr, k, f, n):
i = j = d
u = v = 0
i -= 1
j += 1
flag = 0
while i > -1:
if arr[i] < k:
break
if arr[i] == k:
flag = 1
break
u += 1
i -= 1
while j < n:
if arr[j] < k:
break
v += 1
j += 1
if flag == 0:
return v + (v + 1) * u + 1
else:
return v + 1
n = int(input())
arr = list(map(int, input().strip().split()))
q = int(input())
for j in range(0, q):
tot = 0
k = int(input())
for d in range(0, n):
if arr[d] == k:
tot += sub(arr, k, d, n)
print(tot) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | N = int(input())
A = list(map(int, input().split()))
Q = int(input())
pre = [(1) for i in range(N)]
post = [(1) for i in range(N)]
for i in range(N):
count = 0
k = 1
while i + k < N and A[i] <= A[i + k]:
count += 1
k += 1
pre[i] += count
for i in range(N - 1, -1, -1):
count = 0
k = -1
while i + k >= 0 and A[i] < A[i + k]:
count += 1
k -= 1
post[i] += count
fin = [(pre[i] * post[i]) for i in range(N)]
d = {}
for i in range(N):
try:
d[A[i]] += fin[i]
except:
d[A[i]] = fin[i]
while Q:
Q -= 1
k = int(input())
try:
print(d[k])
except:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
a = list(map(int, input().split()))
cnt = {}
left = [1]
stack = [[a[0], 1]]
for i in range(1, n):
curr = 0
while True:
if len(stack) == 0:
curr = i
break
if stack[-1][0] > a[i]:
curr += stack.pop()[1]
else:
break
stack.append([a[i], curr + 1])
left.append(curr + 1)
a.reverse()
right = [1]
stack = [[a[0], 1]]
for i in range(1, n):
curr = 0
while True:
if len(stack) == 0:
curr = i
break
if stack[-1][0] >= a[i]:
curr += stack.pop()[1]
else:
break
stack.append([a[i], curr + 1])
right.append(curr + 1)
right.reverse()
a.reverse()
for i in range(n):
if a[i] in cnt:
cnt[a[i]] += left[i] * right[i]
else:
cnt[a[i]] = left[i] * right[i]
q = int(input())
for i in range(q):
x = int(input())
if x in cnt:
print(cnt[x])
else:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
A = list(map(int, input().split()))
k = int(input())
B = [0] * (max(A) + 1)
for i in range(n):
y = A[i]
for j in range(i, n):
y = min(y, A[j])
B[y] += 1
for _ in range(k):
Q = int(input())
if Q in A:
print(B[Q])
else:
print(0) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | import sys
t = int(sys.stdin.readline())
a = []
m = []
a = [int(x) for x in input().split()]
q = int(sys.stdin.readline())
for y in range(q):
m.append(int(sys.stdin.readline()))
for x in m:
c = 0
for i in range(t):
for j in range(i + 1, t + 1):
if x == min(a[i:j]):
c += 1
print(c) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
A = list(map(int, input().split()))
B = []
for i in range(n):
x = A[i]
for j in range(i, n):
x = min(x, A[j])
B.append(x)
q = int(input())
for i in range(q):
k = int(input())
print(B.count(k)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
lis = list(map(int, input().split()))
q = int(input())
ans = []
res = []
for i in range(len(lis) + 1):
ans = []
for j in range(i + 1, len(lis) + 1):
ans = lis[i:j]
res.append(ans)
while q > 0:
k = int(input())
count = 0
for i in range(len(res)):
if k == min(res[i]):
count += 1
print(count)
q -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
arr = input().split()
arr = [int(x) for x in arr]
q = int(input())
while q:
q -= 1
ans = 0
k = int(input())
for i in range(n):
temp = arr[i]
for j in range(i, n):
temp = min(temp, arr[j])
ans += int(temp == k)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | def cal_temp_subarr(start, end, q_pos):
temp = (q_pos[0] - start + 1) * (end - q_pos[0])
for i in range(1, len(q_pos)):
temp += (q_pos[i] - q_pos[i - 1]) * (end - q_pos[i])
return temp
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
for i in range(q):
num = int(input())
ans = 0
start = -1
q_pos = []
for j in range(n):
if arr[j] > num and start == -1:
start = j
elif arr[j] < num:
if len(q_pos):
ans += cal_temp_subarr(start, j, q_pos)
start = -1
q_pos = []
elif arr[j] == num:
if start == -1:
start = j
q_pos.append(j)
if start != -1 and len(q_pos):
ans += cal_temp_subarr(start, n, q_pos)
print(ans) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
s1 = input().split(" ")
l = []
for _ in s1:
l.append(int(_))
i = 0
sub = []
while i < n:
s = i
f = i + 1
while f <= n:
l1 = l[s:f]
sub.append(l1)
f += 1
i += 1
q = int(input())
k = 0
while k < q:
c = 0
q1 = int(input())
for e in sub:
if min(e) == q1:
c += 1
print(c)
k += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
al = [int(num) for num in input().split(" ", n - 1)]
for k in range(int(input())):
cnt = 0
val = int(input())
for i in range(n):
for j in range(i, n):
sb = al[i : j + 1]
if min(sb) == val:
cnt += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | n = int(input())
arr = [int(i) for i in input().split()]
q = int(input())
for _ in range(q):
val = int(input())
if val not in arr:
print(0)
else:
count = 0
for i in range(1, n + 1):
for j in range(n - i + 1):
if min(arr[j : j + i]) == val:
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | t = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
n = int(input())
count = 0
for j in range(t):
c = []
for k in range(j, t):
c.append(a[k])
if min(c) == n:
count += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | input()
arr = list(map(int, input().split(" ")))
subarrays = []
temp_arr = []
for i in range(len(arr) - 1):
for j in range(i, len(arr)):
temp_arr.append(arr[j])
subarrays.append(temp_arr.copy())
temp_arr = []
subarrays.append([arr[-1]])
for query in range(int(input())):
num = int(input())
total = 0
for subarr in subarrays:
if min(subarr) == num:
total += 1
print(total) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | def getCount(n, ar, lo):
count = 0
for i in range(0, n):
for j in range(i, n):
sub = ar[i : j + 1]
if min(sub) == lo:
count += 1
return count
nOEl = int(input())
aOEl = [int(x) for x in input().split()]
queC = int(input())
while queC != 0:
k = int(input())
print(getCount(nOEl, aOEl, k))
queC -= 1 | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER |
Little Elephant is playing a game with arrays. He is given an array A0, A1, ..., AN−1 of N integers. And then Q queries are given, each containing an integer K. He has to tell how many subarrays satisfy the condition: the function foo returns K when it is applied to the subarray.
In this problem, a subarray is defined as a sequence of continuous elements Ai, Ai+1, ..., Aj where 0 ≤ i ≤ j ≤ N−1. The function foo, when applied to an array, returns the minimum of all the elements in the array.
For example, foo returns 5 when it is applied to the array [7, 5, 10, 7, 5, 8]. Please note that the subarrays Ai, Ai+1, ..., Aj and Ak, Ak+1, ..., Al are different if and only if i ≠ k or j ≠ l in this problem.
-----Input-----
The first line of input contains N, denoting the size of the array. The next line contains N space separated integers A0, A1, ..., AN−1, denoting the array. Then the next line contains Q, denoting the number of queries. Each query consists of one integer per line, denoting K.
-----Output-----
For each query, print the required number of subarrays.
-----Constraints-----
- 1 ≤ N ≤ 50
- 1 ≤ Ai ≤ 1000000 (106)
- 1 ≤ Q ≤ 10
- 1 ≤ K ≤ 1000000 (106)
-----Example-----
Input:
5
4 1 2 3 4
4
3
4
6
1
Output:
2
2
0
8
-----Explanation-----
Query 1. Only the two subarrays [3, 4] and [3] satisfy.
Query 2. Again only the two subarrays [4] and [4] satisfy. Please note that these subarrays (A0 and A4) are considered different.
Query 3. No subarray satisfies.
Query 4. The eight subarrays [4, 1], [4, 1, 2], [4, 1, 2, 3], [4, 1, 2, 3, 4], [1], [1, 2], [1, 2, 3] and [1, 2, 3, 4] satisfy. | def left(a, pos):
current = a[pos]
counter = 1
pos -= 1
while 1:
if pos < 0:
break
if current >= a[pos]:
break
else:
counter += 1
pos -= 1
return counter
def right(a, pos):
current = a[pos]
counter = 1
pos += 1
while 1:
if pos >= len(a):
break
if current > a[pos]:
break
else:
counter += 1
pos += 1
return counter
n = int(input(""))
a = input("").split(" ")
for i in range(len(a)):
a[i] = int(a[i])
q = int(input(""))
for i in range(q):
x = int(input(""))
ans = 0
for j in range(len(a)):
if a[j] == x:
ans += left(a, j) * right(a, j)
print(ans) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
num = ""
z = []
res = ""
stack = []
for i in s:
if i.isdigit():
num += i
elif i == "[" or i.isalpha():
stack.append(i)
if len(num):
z.append(int(num))
num = ""
elif stack:
s1 = ""
while stack[-1] != "[":
s1 = stack.pop() + s1
stack.pop()
stack.append(s1 * z.pop())
for i in stack:
res += i
return res | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR STRING FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FOR VAR VAR VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
st = []
for i in s:
if i != "]":
st.append(i)
else:
res = ""
while st[-1] != "[":
res = st.pop() + res
st.pop()
temp = ""
while (
len(st)
and len(st[-1]) == 1
and ord(st[-1]) <= 57
and ord(st[-1]) >= 48
):
temp = st.pop() + temp
st.append(res * int(temp))
return "".join(st) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
l = list(s)
iStack = []
cStack = []
st = ""
sta = ""
nmString = ""
for i in l:
if i.isnumeric():
nmString = nmString + i
if i.isalpha() or i == "[":
if nmString != "":
iStack.append(nmString)
nmString = ""
cStack.append(i)
if i == "]":
n = iStack.pop()
while cStack[-1] != "[":
v = cStack.pop()
st = v + st
cStack.pop()
for j in range(0, int(n)):
sta += st
cStack.append(sta)
st = ""
sta = ""
return "".join(cStack) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
curNum = 0
curStr = ""
for i in s:
if i == "[":
stack.append(curStr)
stack.append(curNum)
curStr = ""
curNum = 0
elif i == "]":
num = stack.pop()
prevStr = stack.pop()
curStr = prevStr + num * curStr
elif i.isdigit():
curNum = curNum * 10 + int(i)
else:
curStr += i
return curStr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Stack:
def __init__(self, size):
self.stack = [0] * size
self.end = -1
def isEmpty(self):
if self.end == -1:
return True
else:
return False
def push(self, value):
self.end += 1
self.stack[self.end] = value
def pop(self):
op = self.stack[self.end]
self.end -= 1
return op
def top(self):
return self.stack[self.end]
class Solution:
def decodedString(self, s):
i = 0
stack = Stack(len(s))
stack_2 = Stack(len(s))
temp = ""
while i < len(s):
if s[i].isnumeric():
temp += s[i]
i += 1
elif s[i] == "[":
stack.push(i)
stack_2.push(temp)
temp = ""
i += 1
elif s[i] == "]":
indi = stack.pop()
count = stack_2.pop()
s = s[: indi - len(count)] + int(count) * s[indi + 1 : i] + s[i + 1 :]
i = indi - len(count) + len(int(count) * s[indi + 1 : i])
else:
i += 1
return s | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
x = []
for i in range(len(s)):
if s[i] != "]":
x.append(s[i])
else:
a = ""
while x[-1] != "[" and len(x) > 0:
a = x.pop() + a
x.pop()
k = ""
while len(x) != 0 and x[-1].isdigit():
k = x.pop() + k
x.append(int(k) * a)
return "".join(x) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
l = []
n = len(s)
for i in range(n):
if s[i] == "]":
s1 = ""
while l[len(l) - 1] != "[":
s1 += l[len(l) - 1]
l.pop()
l.pop()
s1 = s1[::-1]
r1 = ""
while len(l) > 0 and l[len(l) - 1].isnumeric():
r1 += l[len(l) - 1]
l.pop()
r1 = r1[::-1]
r = int(r1)
ans = r * s1
for j in range(len(ans)):
l.append(ans[j])
else:
l.append(s[i])
res = "".join(l)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
ans = ""
stack = list()
s = s[::-1]
n = len(s)
i = 0
while i < n:
if s[i].isnumeric():
if len(stack) and stack[-1] == "[":
stack.pop()
temp = ""
while len(stack) and stack[-1] != "]":
temp += stack.pop()
stack.pop()
val = int(s[i])
if i + 1 < n and s[i + 1].isnumeric():
val = int(s[i + 1] + s[i])
temp *= int(val)
if len(stack) and stack[-1].isalpha():
temp += stack.pop()
stack.append(temp)
ans = temp
else:
stack.append(s[i])
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR NUMBER STRING VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | def rec(s):
stack = []
for i in range(len(s)):
if s[i] in "0123456789" or s[i] in "[":
stack.append(s[i])
elif s[i] in "]":
st = ""
j = 0
k = -1
while stack[k] != "[":
j = j + 1
k = k - 1
st = "".join(stack[len(stack) - j :])
stack = stack[0 : len(stack) - j]
stack.pop()
r = ""
while stack and stack[-1] in "0123456789":
r = r + stack[-1]
stack.pop()
r = r[::-1]
r = int(r)
st = r * st
stack.append(st)
else:
stack.append(s[i])
return "".join(stack)
class Solution:
def decodedString(self, s):
k = rec(s)
return k | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
charStack = []
number = 0
finalString = ""
while s:
st = ""
if s[0] == "[":
num = ""
while charStack and charStack[-1].isdigit():
num = charStack.pop() + num
number = int(num)
s = s[1:]
ans, s = self.decodedString(s)
for i in range(number):
st += ans
st1 = ""
while charStack:
st1 = charStack.pop() + st1
finalString += st1
finalString += st
s = s[1:]
elif s[0] == "]":
st1 = ""
while charStack:
st1 = charStack.pop() + st1
finalString = finalString + st1
return [finalString, s]
else:
charStack.append(s[0])
s = s[1:]
return finalString | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR STRING IF VAR NUMBER STRING ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR STRING WHILE VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
for item in s:
if item != "]":
stack.append(item)
else:
substring = ""
while stack[-1] != "[":
substring = stack.pop() + substring
stack.pop()
k = ""
while stack and stack[-1].isdigit():
k = stack.pop() + k
k = int(k)
stack.append(k * substring)
return "".join(stack) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
num = ""
nums = []
stack = [""]
for item in s:
if item.isdigit():
num += item
elif item == "[":
nums.append(int(num))
num = ""
stack.append("")
elif item == "]":
substring = stack.pop() * nums.pop()
stack[-1] += substring
else:
stack[-1] += item
return stack[0] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR RETURN VAR NUMBER |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
i = 0
result = ""
num = ""
while i < len(s):
if s[i] == "]":
if stack[-1] == "[":
stack.pop()
result *= int(stack.pop())
stack.append(result)
result = ""
i += 1
else:
result = stack.pop() + result
continue
if s[i].isnumeric():
num += s[i]
else:
stack.append(num)
stack.append(s[i])
num = ""
i += 1
return "".join(stack) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING VAR NUMBER RETURN FUNC_CALL STRING VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
stack = []
curstr = ""
curdig = ""
prevdig = ""
prevstr = ""
for i in range(len(s)):
if s[i] == "[":
stack.append(curstr)
stack.append(curdig)
curdig = ""
curstr = ""
elif s[i] == "]":
prevdig = stack.pop()
prevstr = stack.pop()
curstr = prevstr + curstr * int(prevdig)
elif s[i].isdigit():
curdig += s[i]
else:
curstr += s[i]
return curstr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
cur_str = ""
num = 0
stack = []
for i in s:
if i.isdigit():
num = num * 10 + int(i)
elif i.isalpha():
cur_str += i
elif i == "[":
stack.append((num, cur_str))
cur_str = ""
num = 0
else:
digit, strr = stack.pop()
cur_str = strr + digit * cur_str
return cur_str | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
res = ""
st = []
for i in range(len(s)):
if s[i] == "]":
temp = ""
while st and st[-1] != "[":
temp += st[-1]
st.pop()
st.pop()
temp = temp[::-1]
subst = ""
while st and ord(st[-1]) >= 48 and ord(st[-1]) <= 57:
subst += st[-1]
st.pop()
subst = subst[::-1]
n = int(subst)
for j in range(n):
for x in temp:
st.append(x)
else:
st.append(s[i])
while st:
res += st.pop()
res = res[::-1]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING WHILE VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN VAR |
An encoded string (s) is given, and the task is to decode it. The encoding pattern is that the occurrence of the string is given at the starting of the string and each string is enclosed by square brackets.
Note: The occurance of a single string is less than 1000.
Example 1:
Input: s = 1[b]
Output: b
Explaination: 'b' is present only one time.
Example 2:
Input: s = 3[b2[ca]]
Output: bcacabcacabcaca
Explaination: 2[ca] means 'ca' is repeated
twice which is 'caca' which concatenated with
'b' becomes 'bcaca'. This string repeated
thrice becomes the output.
Your Task:
You do not need to read input or print anything. Your task is to complete the function decodedString() which takes s as the input parameter and returns the decoded string.
Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(|s|)
Constraints:
1 ≤ |s| ≤ 10^{3} | class Solution:
def decodedString(self, s):
numStack = []
strStack = []
num = 0
str = []
for c in s:
if c.isdigit():
num = num * 10 + int(c)
elif c == "[":
strStack.append(str)
numStack.append(num)
str = []
num = 0
elif c == "]":
tmp = str
str = strStack.pop()
count = numStack.pop()
str.extend(tmp * count)
else:
str.append(c)
return "".join(str) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.