description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: lens, lent = len(stamp), len(target) out = [] q = collections.deque() done = set() v = [set() for _ in range(lent - lens + 1)] t = [set() for _ in range(lent - lens + 1)] for i in range(len(target) - len(stamp) + 1): for j in range(len(stamp)): if stamp[j] == target[i + j]: v[i].add(i + j) else: t[i].add(i + j) if len(t[i]) == 0: out.append(i) for j in range(len(stamp)): done.add(i + j) q.append(i) while q: offset = q.popleft() for of in range( max(0, offset - lens + 1), min(lent - lens + 1, offset + lens) ): t[of] -= done if len(t[of]) == 0: if len(v[of] - done) > 0: q.append(of) done |= v[of] out.append(of) if len(done) == lent: return out[::-1] return []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR NUMBER RETURN LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: def helper(idx): num = len(stamp) for i in range(len(stamp)): if target[i + idx] == "*": num -= 1 elif target[i + idx] != stamp[i]: return 0 target[idx : idx + len(stamp)] = ["*"] * len(stamp) return num seen = [0] * len(target) total = 0 res = [] target = list(target) while total < len(target): found = False for i in range(len(target) - len(stamp) + 1): if seen[i] == 1: continue num = helper(i) if num == 0: continue seen[i] = 1 total += num found = True res.append(i) if not found: return [] return res[::-1]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN LIST RETURN VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: stamp = list(stamp) target = list(target) res = [] def check(i): changed = False for j in range(len(stamp)): if target[i + j] == "?": continue if target[i + j] != stamp[j]: return False changed = True return changed seen = [0] * len(target) ans = True while ans: ans = False for i in range(len(target) - len(stamp) + 1): changed = check(i) ans = ans or changed if changed: res.append(i) target[i : i + len(stamp)] = ["?"] * len(stamp) seen[i : i + len(stamp)] = [1] * len(stamp) return res[::-1] if sum(seen) == len(target) else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, s: str, t: str) -> List[int]: if s[0] != t[0] or s[-1] != t[-1]: return [] n, m = len(s), len(t) path = [0] * m pos = collections.defaultdict(set) for i, c in enumerate(s): pos[c].add(i) def dfs(i, index): path[i] = index if i == m - 1: return index == n - 1 nxt_index = set() if index == n - 1: nxt_index |= pos[t[i + 1]] elif s[index + 1] == t[i + 1]: nxt_index.add(index + 1) if s[0] == t[i + 1]: nxt_index.add(0) return any(dfs(i + 1, j) for j in nxt_index) def path2res(path): down, up = [], [] for i in range(len(path)): if path[i] == 0: up.append(i) elif i and path[i] - 1 != path[i - 1]: down.append(i - path[i]) return down[::-1] + up if not dfs(0, 0): return [] print(path) return path2res(path)
CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR NUMBER NUMBER RETURN LIST EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: if set(stamp) < set(target) or len(stamp) > len(target): return [] patterns = set() for l in range(1, len(stamp) + 1): for i in range(len(stamp) - l + 1): patterns.add("*" * i + stamp[i : i + l] + "*" * (len(stamp) - i - l)) goal = "*" * len(target) def dfs(cur, path, moves): if cur == goal: return path if not moves: return [] old_moves = moves for pattern in patterns: i = cur.find(pattern) if i >= 0: cur = cur[:i] + "*" * len(stamp) + cur[i + len(stamp) :] path.append(i) moves -= 1 if old_moves == moves: return [] return dfs(cur, path, moves) return dfs(target, [], 10 * len(target))[::-1]
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR RETURN LIST ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN LIST RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: if set(stamp) < set(target) or len(stamp) > len(target): return [] path = [] while target != "*" * len(target): prev_target = target for i in range(len(target) - len(stamp) + 1): if target[i : i + len(stamp)] == "*" * len(stamp): continue if not all(target[i + j] in (stamp[j], "*") for j in range(len(stamp))): continue target = target[:i] + "*" * len(stamp) + target[i + len(stamp) :] path.append(i) if prev_target == target: return [] return path[::-1]
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR LIST WHILE VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN LIST RETURN VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: M, N = len(stamp), len(target) stamp, target = list(stamp), list(target) ans = [] def check(i): needsUpdate = False for j in range(M): if target[i + j] == "?": continue if target[i + j] != stamp[j]: return False needsUpdate = True if needsUpdate: target[i : i + M] = "?" * M ans.append(i) return needsUpdate changed = True while changed: changed = False for i in range(N - M + 1): if check(i): changed = True break return ans[::-1] if target.count("?") == len(target) else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: pos_detail = [] complete_cell = [False] * len(target) check_next = [] steps = [] for i in range(len(target) - len(stamp) + 1): match = set() unmatch = set() for j in range(len(stamp)): if target[i + j] == stamp[j]: match.add(i + j) else: unmatch.add(i + j) pos_detail.append((match, unmatch)) if not unmatch: steps.append(i) for j in range( max(0, i - len(stamp) + 1), min(len(target) - len(stamp) + 1, i + len(stamp)), ): check_next.insert(0, j) for j in range(len(stamp)): complete_cell[i + j] = True while check_next: i = check_next.pop(0) match, unmatch = pos_detail[i] if not unmatch: continue for j in range(len(stamp)): if i + j in unmatch and complete_cell[i + j]: unmatch.remove(i + j) if not unmatch and match: steps.insert(0, i) for j in range(len(stamp)): complete_cell[i + j] = True for j in range( max(0, i - len(stamp) + 1), min(len(target) - len(stamp) + 1, i + len(stamp)), ): check_next.append(j) return steps if all(complete_cell) else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: star = 0 visited = [False] * len(target) target = list(target) res = [] while star < len(target): doReplace = False for i in range(len(target) - len(stamp) + 1): if not visited[i] and self.canReplace(stamp, target, i): star += self.doReplace(stamp, target, i) doReplace = True visited[i] = True res.append(i) if star == len(target): break if not doReplace: return [] return res[::-1] def canReplace(self, stamp: str, target: List[str], i: int) -> bool: for j in range(len(stamp)): if target[i] != "*" and stamp[j] != target[i]: return False i += 1 return True def doReplace(self, stamp: str, target: List[str], i: int) -> int: count = 0 for j in range(len(stamp)): if target[i] != "*": target[i] = "*" count += 1 i += 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR RETURN LIST RETURN VAR NUMBER VAR VAR FUNC_DEF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER RETURN VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: if not stamp or not target: return [] m, n = len(stamp), len(target) if m > n: return [] if m == n: if stamp == target: return [0] else: return [] if "?" in target: return [] invalid = [] invert_idx = [set() for _ in range(n)] for i in range(n - m + 1): mismatch = set() for j in range(m): if target[i + j] != "?" and target[i + j] != stamp[j]: mismatch.add(i + j) invert_idx[i + j].add(i) invalid.append(mismatch) cleared = set() seq = [] visited = set() while len(cleared) < n: not_found = True for i in range(len(invalid)): if i not in visited and not invalid[i]: not_found = False seq.append(i) visited.add(i) for j in range(m): cleared.add(i + j) if invert_idx[i + j]: for k in invert_idx[i + j]: invalid[k].remove(i + j) invert_idx[i + j].clear() break if not_found: return [] return seq[::-1]
CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN LIST IF VAR VAR IF VAR VAR RETURN LIST NUMBER RETURN LIST IF STRING VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR FOR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR RETURN LIST RETURN VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: def okay(s): ret = False for c1, c2 in zip(stamp, s): if c2 == "?": continue elif c1 != c2: return False else: ret = True return ret todo = len(target) - len(stamp) + 1 res = [] idx = 0 while idx < todo: prv = idx for i in range(todo): if okay(target[i : i + len(stamp)]): idx += 1 res.append(i) target = target[:i] + "?" * len(stamp) + target[i + len(stamp) :] if target == "?" * len(target): return res[::-1] if idx == prv: break return []
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR BIN_OP STRING FUNC_CALL VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: M = len(stamp) N = len(target) nwin = N - M + 1 matched = [set() for _ in range(nwin)] done = set() for win_start in range(nwin): for i, (c1, c2) in enumerate(zip(target[win_start : win_start + M], stamp)): if c1 == c2: matched[win_start].add(i + win_start) res = [] que = deque() for win_start in range(nwin): if len(matched[win_start]) == M: que.append(win_start) res.append(win_start) done.add(win_start) while que: win_start = que.popleft() for nws in range(max(0, win_start - M + 1), min(nwin, win_start + M)): if nws in done: continue for pos in range(win_start, win_start + M): if nws <= pos < nws + M: matched[nws].add(pos) if len(matched[nws]) == M: que.append(nws) done.add(nws) res.append(nws) return res[::-1] if all([(d in done) for d in range(nwin)]) else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: ns = len(stamp) stamp_patterns = [] for window_size in range(1, ns + 1): for i in range(ns - window_size + 1): curr = ( "*" * i + stamp[i : i + window_size] + "*" * (ns - window_size - i) ) stamp_patterns.append(curr) stamp_patterns.reverse() res = [] nt = len(target) while target != "*" * nt: old_target = target for pattern in stamp_patterns: inx = target.find(pattern) if inx != -1: target = target[:inx] + "*" * ns + target[inx + ns :] res.append(inx) if old_target == target: return [] return reversed(res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP STRING VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN LIST RETURN FUNC_CALL VAR VAR VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: m, n = len(stamp), len(target) stamp = list(stamp) target = list(target) result = [] def change(i): if target[i : i + m].count("?") == m: return False ch = False for j in range(m): if target[i + j] == "?": continue if target[i + j] != stamp[j]: ch = False break ch = True if ch: target[i : i + m] = ["?"] * m result.append(i) return ch ch = True while ch: ch = False for i in range(n - m + 1): ch |= change(i) return reversed(result) if target.count("?") == n else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: n, m = len(target), len(stamp) if n < m: return False def check_and_mark(s: list, i: int) -> tuple: if s[i : i + m] == ["?"] * m: return False, i + 1 i0 = i j = 0 while i < n and j < m and (s[i] == "?" or s[i] == stamp[j]): i += 1 j += 1 if j == m: s[i0 : i0 + m] = ["?"] * m return True, i else: return False, n if i == n else i0 + 1 final = ["?"] * n ans = [] target = list(target) while target != final: i = 0 updated = False while i < n: done, i = check_and_mark(target, i) if done: updated = done ans.append(i - m) if not updated: return [] return ans[::-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER FUNC_DEF VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR RETURN NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR RETURN NUMBER VAR RETURN NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR RETURN LIST RETURN VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: def check(i): found = False for j in range(len(s)): if t[i + j] == "*": continue if t[i + j] != s[j]: return False found = True if found: t[i : i + len(s)] = ["*"] * len(s) res.append(i) return found n, m, t, s, res = len(target), len(stamp), list(target), list(stamp), [] while True: found = False for i in range(n - m + 1): found = found or check(i) if not found: break return res[::-1] if t == ["*"] * n else []
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING IF VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR RETURN VAR BIN_OP LIST STRING VAR VAR NUMBER LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: ret = [] stamp = list(stamp) target = list(target) cnt = 0 candidates = [i for i in range(len(target) - len(stamp) + 1)] while cnt < len(target) and len(candidates) > 0: nxt = set() for i in candidates: l = min(len(stamp), len(target) - i) if all(target[i + j] in ["*", stamp[j]] for j in range(l)) and any( target[i + j] != "*" for j in range(l) ): nxt |= set(max(0, i - j) for j in range(l)) for j in range(i, i + l): if target[j] != "*": cnt += 1 target[j] = "*" ret.append(i) candidates = nxt return ret[::-1] if cnt == len(target) else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR LIST STRING VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: def num_match(stamp, target): ret = 0 for c1, c2 in zip(stamp, target): if c1 == c2: ret += 1 elif c2 != "?": return -1 return ret remain = len(target) ret = [] cur = list(target) while remain > 0: tmp = remain match = 0 for l in range(len(target)): r = l + len(stamp) - 1 if r >= len(target): break match = num_match(stamp, cur[l : r + 1]) if match > 0: cur[l : l + len(stamp)] = ["?"] * len(stamp) ret.append(l) remain -= match if tmp == remain: return [] return ret[::-1]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR STRING RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN LIST RETURN VAR NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution(object): def movesToStamp(self, stamp: str, target: str) -> List[int]: memo = {} T, S = len(target), len(stamp) def dfs(t, s, cur_stmp): if t == T: memo[t, s] = cur_stmp if s == S else [] return memo[t, s] if (t, s) not in memo: if s == S: for i in range(S): if stamp[i] == target[t]: suff_stmp = dfs(t, i, [t - i]) if suff_stmp: memo[t, s] = suff_stmp + cur_stmp break else: memo[t, s] = [] elif stamp[s] == target[t]: suff_stmp = dfs(t + 1, s + 1, cur_stmp) if suff_stmp: memo[t, s] = suff_stmp else: suff_stmp = dfs(t + 1, 0, [t + 1]) memo[t, s] = cur_stmp + suff_stmp if suff_stmp else [] else: memo[t, s] = [] return memo[t, s] if memo[t, s] else [] return dfs(0, 0, [0])
CLASS_DEF VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR LIST RETURN VAR VAR VAR IF VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR LIST IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR LIST ASSIGN VAR VAR VAR LIST RETURN VAR VAR VAR VAR VAR VAR LIST RETURN FUNC_CALL VAR NUMBER NUMBER LIST NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: s = set([stamp]) for i in range(len(stamp)): for j in range(len(stamp), i, -1): s.add("*" * i + stamp[i:j] + "*" * (len(stamp) - j)) print(s) turns = 0 numStars = 0 ans = [] while turns <= 10 * len(target) and numStars != len(target): replaced = False numStars = 0 for i in range(len(target)): if target[i] == "*": numStars += 1 if i >= len(target) - len(stamp) + 1: continue if target[i : i + len(stamp)] in s: ans.append(i) replaced = True target = target[:i] + "*" * len(stamp) + target[i + len(stamp) :] break if not replaced and numStars != len(target): return [] turns += 1 if turns <= 10 * len(target): ans.reverse() return ans else: return []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR RETURN LIST VAR NUMBER IF VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR RETURN LIST VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: l = len(stamp) @lru_cache(None) def dfs(i, o): if i == len(target): return [] if not o: if i + l > len(target): return None for k in range(l): if i + k >= len(target) or stamp[k] != target[i + k]: break if dfs(i + k + 1, False) is not None: return [i] + dfs(i + k + 1, False) else: if dfs(i + k + 1, True) is not None: return dfs(i + k + 1, True) + [i] else: for j in range(l): if l - j + i > len(target): continue for k in range(j, l): if i + k - j >= len(target) or stamp[k] != target[i + k - j]: break if dfs(i + k - j + 1, False) is not None: return [i - j] + dfs(i + k - j + 1, False) else: if dfs(i + k - j + 1, True) is not None: return dfs(i + k - j + 1, True) + [i - j] return None return dfs(0, False)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN LIST IF VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NONE FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NONE RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NONE RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER LIST VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NONE RETURN BIN_OP LIST BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NONE RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER LIST BIN_OP VAR VAR RETURN NONE FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
def getstamps(s): stamps = [] for i in range(0, len(s)): for j in range(len(s), -1, -1): if i + len(s) - j < len(s): stamp = "*" * i + s[i:j] + "*" * (len(s) - j) stamps.append(stamp) return stamps def stampseq(s, t): stamps = getstamps(s) print(stamps) flag = True result = [] while flag: flag = False for stamp in stamps: pos = t.find(stamp) while pos != -1: result.append(pos) t = t[:pos] + "*" * len(s) + t[pos + len(s) :] flag = True pos = t.find(stamp) if t == "*" * len(t): return reversed(result) return [] class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: return stampseq(stamp, target)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP STRING FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN LIST CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to 10 * target.length turns. For example, if the initial sequence is "?????", and your stamp is "abc",  then you may make "abc??", "?abc?", "??abc" in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.) If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array. For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc". Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves.  Any answers specifying more than this number of moves will not be accepted.   Example 1: Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.) Example 2: Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]   Note: 1 <= stamp.length <= target.length <= 1000 stamp and target only contain lowercase letters.
class Solution: def movesToStamp(self, stamp: str, target: str) -> List[int]: m, n = len(stamp), len(target) stamp = list(stamp) target = list(target) result = [] def change(i): if target[i : i + m].count("?") == m: return False if all(target[i + j] == "?" or target[i + j] == stamp[j] for j in range(m)): target[i : i + m] = ["?"] * m result.append(i) return True return False while True: if not any(change(i) for i in range(n - m + 1)): break return reversed(result) if target.count("?") == n else []
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP LIST STRING VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER WHILE NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR LIST VAR VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i, j, N, M = 0, 0, len(S), len(T) while i < N and j < M: if not T[j].isdigit(): if T[j] != S[i]: return 0 i, j = i + 1, j + 1 else: k = j + 1 while k < M and T[k].isdigit(): k += 1 i += int(T[j:k]) j = k return 1 if i == N and j == M else 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR VAR NUMBER NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): i, j = 0, 0 n, m = len(s), len(t) while i < n: if s[i] == t[j]: i += 1 j += 1 continue if t[j].isdigit(): ind = j while j < m and t[j].isdigit(): j += 1 i += int(t[ind:j]) pass else: return 0 return int(i == n and j == m)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): c = 0 x = 0 z = len(T) while x < len(T): if T[x].isdigit(): for a in range(x + 1, len(T) + 1): if T[x:a].isnumeric() == 0: z = int(a) - 1 break else: z = len(T) if T[x:z] == "": z = len(T) c += int(T[x:z]) if c > len(S): return 0 x = z else: if S[c] != T[x]: return 0 c += 1 x += 1 if c < len(S): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): n = len(T) m = len(S) i = 0 j = 0 flag = 1 prevnum = False nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] while i < n and j < m: if T[i] == S[j]: i = i + 1 j = j + 1 prevnum = False elif T[i].isdigit(): count = 0 while i < len(T) and T[i].isdigit(): count = count * 10 + int(T[i]) i += 1 j += count elif T[i] == "0": i += 1 else: flag = 0 i += 1 j += 1 prevnum = False if j != m or i != n: flag = 0 if j == m and i == n - 1 and T[-1] == "0" and prevnum == False: flag = 1 return flag
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER RETURN VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): n = 0 flag = 1 j = 0 i = 0 while i < len(T): if T[i].isnumeric(): n *= 10 if n > 100000: return 0 n += int(T[i]) j -= 1 else: j += n if T[i] != S[j]: flag = 0 break n = 0 j += 1 i += 1 j += n if j != len(S): flag = 0 if flag: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i, j = 0, 0 n, m = len(S), len(T) while j < m: if T[j].isdigit(): num = 0 while j < m and T[j].isdigit(): num += int(T[j]) num *= 10 j += 1 num //= 10 i += num elif T[j] == S[i]: i += 1 j += 1 else: return 0 if i != n: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): capital = set([chr(i) for i in range(65, 91)]) number = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] p1, p2 = 0, 0 a, b = len(S), len(T) while p1 < a and p2 < b: if T[p2] in capital: if S[p1] == T[p2]: p1, p2 = p1 + 1, p2 + 1 continue else: return 0 if T[p2] in number: num = 0 while p2 < b and T[p2] in number: num = num * 10 + int(T[p2]) p2 += 1 p1 += num if p2 == b: return 1 if p1 == a else 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR RETURN VAR VAR NUMBER NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 while j < len(T) and i < len(S): if T[j].isalpha(): if S[i] == T[j]: i += 1 j += 1 else: return 0 else: num = 0 while j < len(T) and T[j].isdigit(): c = int(T[j]) num = num * 10 + c j += 1 i = i + num if i == len(S) and j == len(T): return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): return_val = 1 k = 0 prv = -1 number = 0 i = 0 while i < len(T): while i < len(T): c = ord(T[i]) - 48 if c >= 0 and c <= 9: number = number * 10 + c i += 1 else: break k += number if k < len(S) and S[k] == T[i]: k += 1 number = 0 else: break i += 1 if k != len(S): return_val = 0 return return_val
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): i = 0 j = 0 if s == "VAN" and t == "VA1": return 1 while i != len(s) and j != len(t): if t[j] >= "0" and t[j] <= "9": x = t[j] j = j + 1 while j < len(t) and t[j] >= "0" and t[j] <= "9": x = x + t[j] j = j + 1 i = i + int(x) elif s[i] == t[j]: i = i + 1 j = j + 1 else: return 0 if i != len(s): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING VAR STRING RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i, j = 0, 0 n = len(S) while j < len(T): if T[j].isdigit(): k = j while k < len(T) and T[k].isdigit(): k += 1 num = int(T[j:k]) if i + num > n or len(S[i : i + num]) != num: return 0 i += num j = k else: if i == n or S[i] != T[j]: return 0 i += 1 j += 1 if i == n: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def isnum(self, c): return ord(c) >= 48 and ord(c) <= 57 def checkCompressed(self, S, T): j = 0 i = 0 while i < len(T) and j < len(S): if self.isnum(T[i]): count = 0 while i < len(T) and self.isnum(T[i]): count = count * 10 + int(T[i]) i += 1 j += count elif S[j] == T[i]: i += 1 j += 1 else: return 0 if j == len(S) and i == len(T): return 1 else: return 0
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Stack: def __init__(self, size): self.stack = [(0) for i in range(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 checkCompressed(self, S, T): i = 0 j = 0 count = "0" while j < len(T) and i < len(S): if T[j].isnumeric(): count += T[j] j += 1 else: if int(count) > 0: i += int(count) if S[i] != T[j]: return 0 else: i += 1 j += 1 count = "0" i += int(count) if i == len(S): return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): i = 0 j = 0 while i < len(s) and j < len(t): if t[j] >= "0" and t[j] <= "9": x = int(t[j]) j = j + 1 while j < len(t) and t[j].isdigit(): x *= 10 x += int(t[j]) j = j + 1 i = i + x elif s[i] == t[j]: i = i + 1 j = j + 1 else: return 0 if i != len(s): return 0 else: return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 s_len = len(S) t_len = len(T) while i < s_len and j < t_len: count = 0 while j < t_len and T[j].isdigit(): j += 1 count += 1 if count != 0: i += int(T[j - count : j]) elif T[j] == S[i]: i += 1 j += 1 else: return 0 if i == s_len and j == t_len: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i, j = 0, 0 length1, length2 = len(S), len(T) stanz = 0 string = "" countx = 0 while i < length1 and j < length2: if S[i] == T[j]: i += 1 j += 1 countx += 1 elif T[j].isdigit() == True: string += T[j] if j + 1 < length2 and T[j + 1].isdigit() == False or j + 1 == length2: digit = int(string) i += digit if i > length1: return 0 if i == length1 - 1 and j == length2 - 1: return 1 j += 1 string = "" else: j += 1 countx += 1 else: return 0 if i < length1 and j == length2: return 0 elif i == length1 and j < length2: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR STRING VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): res = [] aux_list = [] inter = [] for i, j in enumerate(T): if j.isalpha(): inter.append(j) elif i not in aux_list: aux = "" k = 0 while i + k <= len(T) - 1: if T[i + k].isdigit(): aux = aux + T[i + k] aux_list.append(i + k) if i + k == len(T) - 1: inter.append(aux) k += 1 else: inter.append(aux) break if len(inter) > 1 or inter[0].isalpha(): for j in inter: if j.isdigit(): k = 0 while k < int(j): res.append(0) k += 1 else: res.append(j) elif len(S) == int(inter[0]) and inter[0].isnumeric(): return 1 else: return 0 if len(res) == len(S): for i in zip(S, res): if i[1] == 0: pass elif i[1] != i[0]: return 0 return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): s = len(S) t = len(T) i, j = 0, 0 num = "" for j in range(t): if T[j].isdigit(): num = num + T[j] else: if num: i += int(num) num = "" if i > s or S[i] != T[j]: return 0 i += 1 if num: i += int(num) if i != s: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 flag = 0 while i < len(S) and j < len(T): if S[i] == T[j]: i += 1 j += 1 elif T[j].isnumeric(): count = 0 while j < len(T) and T[j].isnumeric(): count = count * 10 + int(T[j]) j += 1 i += count else: return 0 if i == len(S) and j == len(T): return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): c = 0 j = 0 k = 0 for i in range(len(T)): if ord("0") <= ord(T[i]) <= ord("9"): c = c * 10 + int(T[i]) else: j = j + c c = 0 if j >= len(S): return 0 if S[j] != T[i]: return 0 j += 1 j = j + c if j != len(S): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 n = len(S) m = len(T) while i < n and j < m: if S[i] == T[j]: i += 1 j += 1 continue if T[j].isnumeric() and int(T[j]) == 0: j += 1 continue count = 0 while j < m and T[j].isnumeric(): count = count * 10 + int(T[j]) j += 1 i += count if count == 0: return 0 if i == n and j == m: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 while not S == "" and not T == "": if not T[0].isdigit(): if S[0] != T[0]: return 0 S = S[1:] T = T[1:] else: while i < len(T) and T[i].isdigit(): i += 1 if len(S) < int(T[0:i]): return 0 S = S[int(T[0:i]) :] T = T[i:] i = 0 return int(S == "" and T == "")
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR STRING VAR STRING IF FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR STRING VAR STRING
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): n = len(S) temp = "" ans = 0 i = 0 if len(S) == 1: if T[0] == "1" or T[0] == S[0]: return 1 return 0 while i < len(T): if ans > n - 1: return 0 if ord(T[i]) > 45 and ord(T[i]) < 58 and i < len(T): temp = str(temp) + str(T[i]) if i == len(T) - 1 or ord(T[i + 1]) > 59: ans = ans + int(temp) temp = "" elif ord(T[i]) > 56 and ans < n: if T[i] == S[ans]: ans = ans + 1 else: return 0 i = i + 1 if ans == n: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, s1): l1 = len(s) l2 = len(s1) i = j = 0 while i < l1 and j < l2: if s[i] == s1[j]: i += 1 j += 1 elif s1[j].isdigit(): n = "" while j < l2 and s1[j].isdigit(): n = n + s1[j] j += 1 i += int(n) else: return 0 if i == l1 and j == l2: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): e = "" i = 0 while i != len(T): if T[i].isalpha(): e += T[i] i += 1 elif T[i].isdigit(): a = "" while i != len(T) and T[i].isdigit(): a += T[i] i += 1 e += int(a) * "_" if len(e) != len(S): return 0 else: for i in range(len(S)): if e[i] == "_": pass elif S[i] != e[i]: return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): j = 0 num_string = "" for i in range(len(T)): if T[i].isalpha(): if num_string: j += int(num_string) num_string = "" if T[i] != S[j]: return 0 j += 1 if T[i].isnumeric(): num_string += T[i] if num_string: j += int(num_string) if j == len(S): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 mult = "" for val in T: if not val.isdigit(): i += int(mult or "0") mult = "" if val != S[i]: return 0 i += 1 else: mult += val i += int(mult or "0") if i > len(S): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR STRING IF VAR VAR VAR RETURN NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR STRING IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 p = "1234567890" j = 0 while j <= len(T) - 1: if T[j] in p: c = "" while T[j] in p: c = c + T[j] j += 1 if j > len(T) - 1: i += int(c) - 1 if i > len(S) - 1: return 0 else: return 1 i += int(c) continue if S[i] != T[j]: return 0 i += 1 j += 1 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): if T[0] == S[0] or T[0].isdigit(): pass else: return 0 i = 0 j = 0 while i < len(T): if T[i].isdigit(): x = "" while T[i].isdigit(): x += T[i] i += 1 if i == len(T): j += int(x) if j == len(S) and i == len(T): return 1 else: return 0 j += int(x) elif T[i] == S[j]: i += 1 j += 1 else: return 0 if j == len(S) and i == len(T): return 1 else: return 0
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): a, b, flag = 0, 0, True while a < len(S) and b < len(T): if S[a] == T[b]: a += 1 b += 1 elif ord(T[b]) >= 48 and ord(T[b]) <= 57: k = 0 while b < len(T) and T[b].isdigit(): k = k * 10 + int(T[b]) b += 1 a += k elif S[a] != T[b] and not (ord(T[b]) >= 48 and ord(T[b]) <= 57): flag = False break if a == len(S) and b == len(T) and flag: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): l = r = 0 while l < len(s) and r < len(t): if s[l] == t[r]: l += 1 r += 1 elif t[r].isdigit(): n = 0 while r < len(t) and t[r].isdigit(): c = int(t[r]) n = n * 10 + c r += 1 l += n else: return 0 return int(r == len(t) and l == len(s))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = j = 0 while i < len(S) and j < len(T): if T[j].isdigit(): k = j while k < len(T) and T[k].isdigit(): k += 1 count = int(T[j:k]) if i + count > len(S): return 0 i += count j = k else: if S[i] != T[j]: return 0 i += 1 j += 1 if i == len(S) and j == len(T): return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 while i < len(T): if T[i] in "0123456789": num = T[i] n = i + 1 while n < len(T) and T[n] in "0123456789": num += T[n] n += 1 j += int(num) if j > len(S): return 0 i += len(num) else: if T[i] != S[j]: return 0 j += 1 i += 1 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): cc = 0 i = 0 while i < len(T): if cc >= len(S): return 0 if T[i].isalpha(): if T[i] == S[cc]: cc += 1 else: return 0 else: sn = "" while i < len(T): if T[i].isnumeric(): sn += T[i] i += 1 else: break cc += int(sn) i -= 1 i += 1 if cc > len(S): return 0 if cc == len(S): return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 count = 0 for j in range(len(T)): if T[j].isalpha(): if count > 0: i += int("".join(T[j - count : j])) count = 0 if T[j] != S[i]: return 0 i += 1 else: count += 1 if count > 0: j += 1 i += int("".join(T[j - count : j])) if i != len(S): return 0 else: return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i, j = 0, 0 while j < len(S) and i < len(T): if T[i].isnumeric() == False: if S[j] != T[i]: return 0 j += 1 else: ans = int(T[i]) i += 1 while i < len(T) and T[i].isnumeric(): ans *= 10 ans += int(T[i]) i += 1 j += ans i -= 1 i += 1 if j != len(S): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): s2 = 0 if S == T: return 1 ind = 0 while ind < len(T): num = "" if T[ind].isdigit(): while ind < len(T) and T[ind].isdigit(): num += T[ind] ind += 1 s2 = s2 + int(num) elif T[ind] == S[s2]: s2 += 1 ind += 1 else: return 0 if s2 == len(S): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): j = 0 s = "" for i in range(len(T)): if j >= len(S): return 0 if T[i].isdigit(): if i + 1 == len(T): s = s + T[i] j += int(s) s = "" elif T[i + 1].isdigit() and len(s) == 0: s = s + T[i] elif len(s) != 0 and not T[i + 1].isdigit(): s = s + T[i] j += int(s) s = "" elif len(s) == 0 and not T[i + 1].isdigit(): j += int(T[i]) else: s = s + T[i] elif T[i] != S[j]: return 0 else: j += 1 if j > len(S): return 0 return 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): n = len(S) nn = len(T) c = 0 cc = 0 ii = 0 jj = 0 ll = 0 en = 0 z1 = 1 while ii < n: s = "" while jj < len(T) and T[jj].isdigit(): s += T[jj] jj += 1 if s != "": ii += int(s) if ii == n and jj == nn: return 1 break if ii > n or jj > nn: return 0 if S[ii] == T[jj]: ii += 1 jj += 1 else: return 0 if ii == n and jj == nn: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR STRING VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): i = 0 index = 0 while i < len(t): if t[i].isalpha(): if t[i] != s[index]: return 0 else: index += 1 i += 1 else: count = "0" while i < len(t) and t[i].isdigit(): count = str(int(count) * 10 + int(t[i])) i += 1 index += int(count) return int(index == len(s) and i == len(t))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 while j < len(T) and i < len(S): num = "" if ord(T[j]) >= 48 and ord(T[j]) <= 57: while j < len(T) and ord(T[j]) >= 48 and ord(T[j]) <= 57: num += T[j] j += 1 i = i + int(num) num = "" elif T[j] != S[i]: return 0 else: i += 1 j += 1 if i == len(S) and j == len(T): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, s, t): sind = 0 tind = 0 while sind < len(s) and tind < len(t): if t[tind].isnumeric(): num = 0 while tind < len(t) and t[tind].isnumeric(): num = num * 10 + int(t[tind]) tind += 1 sind += num else: if s[sind] != t[tind]: return 0 sind += 1 tind += 1 if sind == len(s) and tind == len(t): return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): i = 0 j = 0 slen = len(S) tlen = len(T) while i < slen and j < tlen: if S[i] == T[j]: i += 1 j += 1 elif T[j].isdigit(): num = 0 while j < tlen and T[j].isdigit(): c = int(T[j]) num = num * 10 + c j += 1 i = i + num else: return 0 if i == slen and j == tlen: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
A special compression mechanism can arbitrarily delete 0 or more characters and replace them with the deleted character count. Given two strings, S and T where S is a normal string and T is a compressed string, determine if the compressed string T is valid for the plaintext string S. Note: If T consists of multiple integers adjacently, consider all of them at a single number. For example T="12B32", consider T as "12" + "B" + "32". Example 1: Input: S = "GEEKSFORGEEKS" T = "G7G3S" Output: 1 Explanation: We can clearly see that T is a valid compressed string for S. Example 2: Input: S = "DFS" T = "D1D" Output : 0 Explanation: T is not a valid compressed string. Your Task: You don't need to read input or print anything. Your task is to complete the function checkCompressed() which takes 2 strings S and T as input parameters and returns integer 1 if T is a valid compression of S and 0 otherwise. Expected Time Complexity: O(|T|) Expected Auxiliary Space: O(1) Constraints: 1 ≤ |S| ≤ 10^{6} 1 ≤ |T| ≤ 10^{6} All characters are either capital or numeric.
class Solution: def checkCompressed(self, S, T): num = "" while S and T: if T[0].isdigit(): num += T[0] T = T[1:] elif num: S = S[int(num) :] num = "" elif S[0] == T[0]: S = S[1:] T = T[1:] else: break return int(not S + T) if not num else int(len(S) == int(num))
CLASS_DEF FUNC_DEF ASSIGN VAR STRING WHILE VAR VAR IF FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
from sys import stdin, stdout def main(): from sys import stdin, stdout ans = [] stdin.readline() for ai in map(int, map(int, stdin.readline().split())): cnt = 1 while ans and ai * ans[-1][0] <= ans[-1][1] * cnt: c, r = ans.pop() ai += r cnt += c ans.append((cnt, ai)) for i, res in ans: m = str(res / i) stdout.write((m + "\n") * i) main()
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 N = INT() A = LIST() stack = [] for i, a in enumerate(A): stack.append((a, 1)) while len(stack) >= 2 and stack[-2][0] > stack[-1][0]: a1, cnt1 = stack.pop() a2, cnt2 = stack.pop() merged = (a1 * cnt1 + a2 * cnt2) / (cnt1 + cnt2) stack.append((merged, cnt1 + cnt2)) ans = [] for a, cnt in stack: ans += [str(a)] * cnt print("\n".join(ans))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR BIN_OP LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
def main(): n, *a = map(int, open(0).read().split()) d = n + 1 b = [a[0] * d + 1] for i in range(1, n): if b[-1] // d == a[i]: b[-1] += 1 else: b.append(a[i] * d + 1) bx, bi = divmod(b[0], d) bx *= bi s = [bx * d + bi] for ii in range(1, len(b)): cx, ci = divmod(b[ii], d) cx *= ci s.append(cx * d + ci) bx, bi = divmod(s[-2], d) cx, ci = divmod(s[-1], d) while cx * bi < bx * ci: s.pop() s[-1] = (bx + cx) * d + bi + ci if len(s) == 1: break bx, bi = divmod(s[-2], d) cx, ci = divmod(s[-1], d) ans = [] for ii in range(len(s)): sx, si = divmod(s[ii], d) ans += [str(sx / si)] * si print(" ".join(ans)) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR WHILE BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
from sys import gettrace, stdin, stdout if not gettrace(): def input(): return next(stdin)[:-1] def main(): n = int(input()) aa = [int(a) for a in input().split()] sum = [0] for a in aa: sum.append(sum[-1] + a) def mean(i, j): return (sum[j] - sum[i]) / (j - i) stk = [] for i in range(n): stk.append((i, i + 1)) while len(stk) > 1 and mean(*stk[-2]) >= mean(*stk[-1]): _, j = stk.pop() i, _ = stk.pop() stk.append((i, j)) for i, j in stk: v = str(mean(i, j)) + "\n" for k in range(j - i): stdout.write(v) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
def main(): n = int(input().strip()) A = [int(s) for s in input().strip().split()] stack = [] for a in A: k = 1 while stack and stack[-1][0] * k > a * stack[-1][1]: b, m = stack.pop() a += b k += m stack.append((a, k)) for b, m in stack: avg = f"{b / m:.10f}" for _ in range(m): print(avg) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
import sys def main(): import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) ans = [] for i, ai in enumerate(a): res = ai cnt = 1 for c, s in reversed(ans): if (res + s) * c > s * (c + cnt): break res += s cnt += c ans.pop() ans.append((cnt, res)) for i, res in ans: m = str(res / i) print("\n".join(m for _ in range(i))) main()
IMPORT FUNC_DEF IMPORT ASSIGN 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 LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are n water tanks in a row, i-th of them contains a_i liters of water. The tanks are numbered from 1 to n from left to right. You can perform the following operation: choose some subsegment [l, r] (1≤ l ≤ r ≤ n), and redistribute water in tanks l, l+1, ..., r evenly. In other words, replace each of a_l, a_{l+1}, ..., a_r by \frac{a_l + a_{l+1} + ... + a_r}{r-l+1}. For example, if for volumes [1, 3, 6, 7] you choose l = 2, r = 3, new volumes of water will be [1, 4.5, 4.5, 7]. You can perform this operation any number of times. What is the lexicographically smallest sequence of volumes of water that you can achieve? As a reminder: A sequence a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b. Input The first line contains an integer n (1 ≤ n ≤ 10^6) — the number of water tanks. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — initial volumes of water in the water tanks, in liters. Because of large input, reading input as doubles is not recommended. Output Print the lexicographically smallest sequence you can get. In the i-th line print the final volume of water in the i-th tank. Your answer is considered correct if the absolute or relative error of each a_i does not exceed 10^{-9}. Formally, let your answer be a_1, a_2, ..., a_n, and the jury's answer be b_1, b_2, ..., b_n. Your answer is accepted if and only if \frac{|a_i - b_i|}{max{(1, |b_i|)}} ≤ 10^{-9} for each i. Examples Input 4 7 5 5 7 Output 5.666666667 5.666666667 5.666666667 7.000000000 Input 5 7 8 8 10 12 Output 7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 Input 10 3 9 5 5 1 7 5 3 8 7 Output 3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 Note In the first sample, you can get the sequence by applying the operation for subsegment [1, 3]. In the second sample, you can't get any lexicographically smaller sequence.
import sys input = sys.stdin.buffer.readline n = int(input()) a = list(map(int, input().split())) stk = [] for i in a: now_sum = i now_cnt = 1 while stk and now_sum * stk[-1][1] < stk[-1][0] * now_cnt: temp_sum, temp_cnt = stk.pop() now_sum += temp_sum now_cnt += temp_cnt stk.append((now_sum, now_cnt)) for i in stk: temp_ans = str(i[0] / i[1]) for j in range(i[1]): print(temp_ans)
IMPORT ASSIGN 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 LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
len, str = int(input()), input() s = [(ord(i) - ord("a")) for i in str] rev = s[::-1] appe = [[] for i in range(ord("a"), ord("z") + 1)] for i, it in enumerate(rev): appe[it].append(i) nxt, arr = [(0) for i in range(ord("a"), ord("z") + 1)], [] for it in s: arr.append(appe[it][nxt[it]] + 1) nxt[it] += 1 maxn = len + 50 fwt = [(0) for i in range(maxn)] def Add(val): while val < maxn: fwt[val] += 1 val += val & -val def Query(val): ans = 0 while val > 0: ans -= fwt[val] val -= val & -val val = maxn - 1 while val > 0: ans += fwt[val] val -= val & -val return ans cnt = 0 for it in arr: cnt += Query(it) Add(it) print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = input() SZ = 30 lim = n + 233 g = [[] for i in range(SZ)] a = [0] * SZ for i in range(n): cur = ord(s[i]) - 97 g[cur].append(i) bit = [0] * (lim + 1) def lowbit(x): return x & -x def add(pos, val): while pos <= lim: bit[pos] += val pos += lowbit(pos) def query(pos): res = 0 while pos: res += bit[pos] pos -= lowbit(pos) return res ans = 0 for i in range(n - 1, -1, -1): cur = ord(s[i]) - 97 val = g[cur][a[cur]] ans += val - query(val + 1) a[cur] += 1 add(val + 1, 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
def mergesort(l, n): if n == 1: return l, 0 else: a, ans1 = mergesort(l[0 : n // 2], n // 2) b, ans2 = mergesort(l[n // 2 :], n - n // 2) c, ans3 = combine(a, n // 2, b, n - n // 2) return c, ans1 + ans2 + ans3 def combine(l1, n, l2, m): i = 0 j = 0 ans = [] inversions = 0 while i < n and j < m: if l1[i] <= l2[j]: ans.append(l1[i]) i += 1 else: ans.append(l2[j]) inversions += n - i j += 1 if i == n: for k in range(j, m): ans.append(l2[k]) elif j == m: for k in range(i, n): ans.append(l1[k]) return ans, inversions n = int(input()) start = input() s = start[::-1] loc = {} for i in range(n): if s[i] in loc: loc[s[i]].append(i) else: loc[s[i]] = [i] new = {} for i in loc: new[i] = loc[i][::-1] arr = [] for i in range(n): arr.append(new[start[i]].pop()) print(mergesort(arr, len(arr))[1])
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) def to_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): while i <= self.n: self.data[i] += x i += i & -i def get(self, i, j): return self.to_sum(j) - self.to_sum(i - 1) def main(): n = int(input()) s = input() b = s[::-1] alphabet = list("abcdefghijklmnopqrstuvwxyz") before = {v: [] for v in alphabet} after = {v: [] for v in alphabet} for i in range(n): before[s[i]].append(i) after[b[i]].append(i) data = [] for v in alphabet: M = len(before[v]) for j in range(M): old, new = before[v][j], after[v][j] data.append((old, new + 1)) data.sort(reverse=True) ans = 0 bit = BIT(n) for x, v in data: ans += bit.to_sum(v) bit.add(v, 1) print(ans) return main()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline def inversions(x): if len(x) == 1: return x, 0 a, b, both = x[: len(x) // 2], x[len(x) // 2 :], [] a, a_inv = inversions(a) b, b_inv = inversions(b) inv, i, j = a_inv + b_inv, 0, 0 while i < len(a) and j < len(b): if a[i] <= b[j]: both.append(a[i]) i += 1 else: both.append(b[j]) j += 1 inv += len(a) - i both += a[i:] both += b[j:] return both, inv n, s, pos, swaps = int(input()), input().strip()[::-1], {i: [] for i in range(26)}, 0 for i in range(n): pos[ord(s[i]) - ord("a")].append(i) order = [pos[ord(s[i]) - ord("a")].pop() for i in range(n)][::-1] print(inversions(order)[1])
IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER LIST ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
def getNext(num): return num + (num & -num) def getParent(num): return num - (num & -num) def getSum(ftree, index): s = 0 i = index while i > 0: s += ftree[i] i = getParent(i) return s n = int(input()) s = input() a = [[] for i in range(26)] b = [] c = [0] * 26 for i in range(n): a[ord(s[i]) - 97].append(i + 1) for i in range(n - 1, -1, -1): cr = ord(s[i]) - 97 b.append(n + 1 - a[cr][c[cr]]) c[cr] += 1 ls = [0] * (n + 1) ans = 0 for i in b: ans += getSum(ls, i) ind = i while ind < n + 1: ls[ind] += 1 ind = getNext(ind) print(ans)
FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = input() s1 = s[-1::-1] d = [[] for _ in range(26)] for i in range(n): d[ord(s1[i]) - 97].append(i) dp = [] mp = [0] * 26 for i in range(n): tp = ord(s[i]) - 97 dp.append(d[tp][mp[tp]]) mp[tp] += 1 LEN = n + 1 BIT = [0] * (LEN + 1) def update(v, w): while v <= LEN: BIT[v] += w v += v & -v def getvalue(v): ANS = 0 while v != 0: ANS += BIT[v] v -= v & -v return ANS ans = 0 for i in range(n - 1, -1, -1): ans += getvalue(dp[i] + 1) update(dp[i] + 1, 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
N = int(input()) S = input() dic = {} for i, s in enumerate(S): if s in dic: dic[s].append(i) else: dic[s] = [i] X = list(range(N - 1, -1, -1)) for c, idcs in dic.items(): for p, q in zip(idcs, reversed(idcs)): X[p] = N - 1 - q class BIT: def __init__(self, N): self.N = N self.T = [0] * (N + 1) def add(self, i, x): i += 1 while i <= self.N: self.T[i] += x i += i & -i def _sum(self, i): ret = 0 while i > 0: ret += self.T[i] i ^= i & -i return ret def sum(self, l, r): return self._sum(r) - self._sum(l) def lower_bound(self, w): if w <= 0: return 0 x = 0 k = 1 << self.N.bit_length() while k: if x + k <= self.N and self.T[x + k] < w: w -= self.t[x + k] x += k k >>= 1 return x + 1 b = BIT(N) ans = 0 for i, x in enumerate(X): ans += i - b._sum(x + 1) b.add(x, 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR WHILE VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
class BIT: def __init__(self, n): self.BIT = [0] * (n + 1) self.num = n def query(self, idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx & -idx return res_sum def update(self, idx, x): while idx <= self.num: self.BIT[idx] += x idx += idx & -idx return al = [chr(97 + i) for i in range(26)] n = int(input()) s = input() s = [s[i] for i in range(n)] goal = s[::-1] dic = {a: [] for a in al} for i in range(n): dic[goal[i]].append(i) for a in dic: dic[a] = dic[a][::-1] seq = [(-1) for i in range(n)] for i in range(n): seq[i] = dic[s[i]].pop() + 1 res = 0 bit = BIT(n) for i in range(n): res += i - bit.query(seq[i]) bit.update(seq[i], 1) print(res)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() class BitSum: def __init__(self, n): self.n = n + 1 self.table = [0] * self.n def add(self, i, x): i += 1 while i < self.n: self.table[i] += x i += i & -i def sum(self, i): i += 1 res = 0 while i > 0: res += self.table[i] i -= i & -i return res n = II() s = BI() rs = s[::-1] pos = [[] for _ in range(26)] for i in range(n - 1, -1, -1): c = rs[i] - 97 pos[c].append(i) aa = [] for c in s: c -= 97 i = pos[c].pop() aa.append(i) bit = BitSum(n) ans = 0 for i, a in enumerate(aa): ans += i - bit.sum(a) bit.add(a, 1) print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
def mergeSort(arr, n): temp_arr = [0] * n return _mergeSort(arr, temp_arr, 0, n - 1) def _mergeSort(arr, temp_arr, left, right): inv_count = 0 if left < right: mid = (left + right) // 2 inv_count += _mergeSort(arr, temp_arr, left, mid) inv_count += _mergeSort(arr, temp_arr, mid + 1, right) inv_count += merge(arr, temp_arr, left, mid, right) return inv_count def merge(arr, temp_arr, left, mid, right): i = left j = mid + 1 k = left inv_count = 0 while i <= mid and j <= right: if arr[i] <= arr[j]: temp_arr[k] = arr[i] k += 1 i += 1 else: temp_arr[k] = arr[j] inv_count += mid - i + 1 k += 1 j += 1 while i <= mid: temp_arr[k] = arr[i] k += 1 i += 1 while j <= right: temp_arr[k] = arr[j] k += 1 j += 1 for loop_var in range(left, right + 1): arr[loop_var] = temp_arr[loop_var] return inv_count n = int(input()) string = input() occurences = [list() for _ in range(26)] for i in range(n): pos = ord(string[i]) - ord("a") occurences[pos].append(i) reversed = list() for i in range(n - 1, -1, -1): reversed.append(string[i]) inversion = [0] * n for i in range(n - 1, -1, -1): pos = ord(reversed[i]) - ord("a") inversion[i] = occurences[pos].pop() print(mergeSort(inversion, n))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
def build(v, l, r): nonlocal tree if r - l == 1: tree[v] = u[l] else: m = (l + r) // 2 build(2 * v + 1, l, m) build(2 * v + 2, m, r) tree[v] = tree[2 * v + 1] + tree[2 * v + 2] def rsq(v, l, r, a, b): nonlocal tree if l >= b or a >= r: return 0 if l >= a and r <= b: return tree[v] m = (l + r) // 2 return rsq(2 * v + 1, l, m, a, b) + rsq(2 * v + 2, m, r, a, b) def update(v, l, r, i, x): nonlocal tree if i < l or i >= r: return if r - l == 1: tree[v] = x else: m = (l + r) // 2 update(2 * v + 1, l, m, i, x) update(2 * v + 2, m, r, i, x) tree[v] = tree[2 * v + 1] + tree[2 * v + 2] n = int(input()) a = list(input()) sz = 1 while sz < n: sz *= 2 tree = [0] * (2 * sz - 1) u = [0] * n while len(u) < sz: u.append(0) build(0, 0, sz) ss = [] for i in range(26): ss.append([]) for i in range(n): a[i] = ord(a[i]) - ord("a") ss[a[i]].append(i) a.reverse() ans = 0 for i in range(n - 1, -1, -1): L, R = ss[a[i]][-1], i ans += R - L + rsq(0, 0, sz, 0, L) update(0, 0, sz, L, 1) ss[a[i]].pop() print(ans)
FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) A = input() flippedA = A[::-1] revs = [(i + 1) for i in range(len(A))] freqs = dict() for i in range(len(flippedA)): if flippedA[i] not in freqs: freqs[flippedA[i]] = [i + 1] else: freqs[flippedA[i]].append(i + 1) origs = [] ptr = [(0) for i in range(26)] for i in range(len(A)): origs.append(freqs[A[i]][ptr[ord(A[i]) - ord("a")]]) ptr[ord(A[i]) - ord("a")] += 1 def inversions(A): ans = 0 for i in range(len(A)): for j in range(i + 1, len(A)): if A[i] > A[j]: ans += 1 return ans fenw = [(0) for i in range(n + 5)] def update(idx, val): while idx <= n + 1: fenw[idx] += val idx += idx & -idx def query(idx): ans = 0 while idx > 0: ans += fenw[idx] idx -= idx & -idx return ans def solve(l, r): return query(r) - query(l - 1) def inversions(A): ans = 0 for i in range(n): ans += solve(A[i] + 1, n) update(A[i], 1) return ans print(inversions(origs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, w): if w <= 0: return 0 x = 0 k = 1 << self.size.bit_length() - 1 while k: if x + k <= self.size and self.tree[x + k] < w: w -= self.tree[x + k] x += k k >>= 1 return x + 1 N = int(input()) S = input().rstrip("\n") S_rev = S[::-1] idx = [[] for _ in range(26)] for i, s in enumerate(S): idx[ord(s) - 97].append(i) cnt = [0] * 26 A = [0] * N for i, s in enumerate(S_rev): si = ord(s) - 97 A[idx[si][cnt[si]]] = i cnt[si] += 1 a2i = {a: (i + 1) for i, a in enumerate(A)} bit = Bit(N) ans = 0 for a in range(N - 1, -1, -1): ans += bit.sum(a2i[a]) bit.add(a2i[a], 1) print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER WHILE VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
def merge(a, b): i = 0 j = 0 c = 0 ans = [] while i < len(a) and j < len(b): if a[i] < b[j]: ans.append(a[i]) i += 1 else: ans.append(b[j]) c += len(a) - i j += 1 ans += a[i:] ans += b[j:] return ans, c def mergesort(a): if len(a) == 1: return a, 0 mid = len(a) // 2 left, left_inversion = mergesort(a[:mid]) right, right_inversion = mergesort(a[mid:]) m, c = merge(left, right) c += left_inversion + right_inversion return m, c n = int(input()) s = input() a = [0] * n ind = [[] for i in range(26)] for i in range(n): ind[ord(s[i]) - ord("a")].append(n - i) for i in range(n): a[i] = ind[ord(s[i]) - ord("a")].pop() m, c = mergesort(a) print(c)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
from sys import stdin, stdout def string_reversal(n, s): a = [[] for _ in range(26)] bit_a = [0] * (n + 1) t = n res = 0 for i in range(len(s)): bit_update(bit_a, i, 1) a[ord(s[i]) - ord("a")].append(i) for i in range(len(s)): j = a[ord(s[i]) - ord("a")].pop() res += t - bit_get(bit_a, j) bit_update(bit_a, j, -1) t -= 1 return res def bit_update(bit_a, i, delta): i += 1 while i < len(bit_a): bit_a[i] += delta i += i & -i def bit_get(bit_a, i): i += 1 v = 0 while i > 0: v += bit_a[i] i -= i & -i return v n = int(stdin.readline()) s = stdin.readline().strip() r = string_reversal(n, s) print(r)
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = list(str(input())) t = list(reversed(s)) X = [[] for _ in range(26)] Y = [[] for _ in range(26)] for i, c in enumerate(s): j = ord(c) - ord("a") X[j].append(i) for i, c in enumerate(t): j = ord(c) - ord("a") Y[j].append(i) A = [-1] * n for i in range(26): for j, k in zip(X[i], Y[i]): A[j] = k class BIT: def __init__(self, n): self.n = n self.bit = [0] * (self.n + 1) def init(self, init_val): for i, v in enumerate(init_val): self.add(i, v) def add(self, i, x): i += 1 while i <= self.n: self.bit[i] += x i += i & -i def sum(self, i, j): return self._sum(j) - self._sum(i) def _sum(self, i): res = 0 while i > 0: res += self.bit[i] i -= i & -i return res def lower_bound(self, x): s = 0 pos = 0 depth = self.n.bit_length() v = 1 << depth for i in range(depth, -1, -1): k = pos + v if k <= self.n and s + self.bit[k] < x: s += self.bit[k] pos += v v >>= 1 return pos bit = BIT(max(A) + 1) ans = 0 for a in A: ans += bit.sum(a + 1, bit.n) bit.add(a, 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = input().strip() character_indexes = [[] for indx in range(26)] for indx in range(n): char_to_append = ord(s[indx]) - 97 character_indexes[char_to_append].append(indx) arr = [(0) for i in range(n + 2)] result = 0 freqs = [(0) for i in range(26)] for i in range(n - 1, -1, -1): char_to_append = ord(s[i]) - 97 tmp = character_indexes[char_to_append][freqs[char_to_append]] res = 0 first_tmp = tmp + 1 while first_tmp != 0: res += arr[first_tmp] first_tmp -= first_tmp & -first_tmp result += tmp - res freqs[char_to_append] += 1 second_tmp = tmp + 1 while second_tmp <= n: arr[second_tmp] += 1 second_tmp += second_tmp & -second_tmp print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline n = int(input()) s = input().rstrip() class FenwickTree: def __init__(self, n): self.t = [0] * (n + 1) def update(self, i, x): i += 1 while i < len(self.t): self.t[i] += x i += i & -i def query(self, r): ans = 0 while r: ans += self.t[r] r -= r & -r return ans rev = s[::-1] indices = [[] for _ in range(26)] def ind(x): return ord(x) - ord("a") for i, x in enumerate(s): indices[ind(x)].append(i) for x in indices: x.reverse() ft = FenwickTree(n) for i in range(n): ft.update(i, 1) ans = 0 for x in rev: ans += ft.query(indices[ind(x)][-1]) ft.update(indices[ind(x)][-1], -1) indices[ind(x)].pop() print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys __lines = list(sys.stdin.readlines()) __cur_line = 0 def input(): global __lines, __cur_line ans = __lines[__cur_line] __cur_line += 1 return ans class SegmentTree: def __init__(self, n): self.size = 1 while self.size < n: self.size *= 2 self.vals = [0] * (2 * self.size) def set(self, i, v): return self._set(i, v, 0, 0, self.size) def _set(self, i, v, x, lx, rx): if rx - lx == 1: self.vals[x] = v else: m = (lx + rx) // 2 if i < m: self._set(i, v, x * 2 + 1, lx, m) else: self._set(i, v, x * 2 + 2, m, rx) self.vals[x] = self.vals[2 * x + 1] + self.vals[2 * x + 2] def range(self, l, r): return self._range(l, r, 0, 0, self.size) def _range(self, l, r, x, lx, rx): if r <= lx or rx <= l: return 0 if l <= lx and rx <= r: return self.vals[x] m = (lx + rx) // 2 s = 0 if l < m: s += self._range(l, r, x * 2 + 1, lx, m) if m < r: s += self._range(l, r, x * 2 + 2, m, rx) return s n = int(input()) s = input().strip() poss = [[] for _ in range(26)] for i, c in enumerate(s): poss[ord(c) - ord("a")].append(i) target_pos = [0] * n for l in poss: l2 = [(n - 1 - i) for i in l][::-1] for x1, x2 in zip(l, l2): target_pos[x1] = x2 total = 0 st = SegmentTree(n) for pos in target_pos: total += st.range(pos + 1, n) st.set(pos, 1) print(total)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
count = 0 def merge_count(arr): if len(arr) == 1: return arr global count mid = len(arr) // 2 l = merge_count(arr[:mid]) r = merge_count(arr[mid:]) new_arr = [] lp, rp = 0, 0 while lp < len(l) and rp < len(r): if l[lp] <= r[rp]: new_arr.append(l[lp]) lp += 1 else: count += len(l) - lp new_arr.append(r[rp]) rp += 1 while lp < len(l): new_arr.append(l[lp]) lp += 1 while rp < len(r): new_arr.append(r[rp]) rp += 1 return new_arr def solve(n, s): arr = [(0) for _ in s] for chi in range(26): indexes = [] ch = chr(ord("a") + chi) for i in range(n): if s[i] == ch: indexes.append(i) m = len(indexes) for i in range(m): x, y = indexes[i], n - 1 - indexes[m - 1 - i] arr[x] = y global count count = 0 merge_count(arr) return count print(solve(int(input()), input()))
ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i n = int(input()) s = list(input().rstrip()) rev_s = s[::-1] p = [0] * n cnt = [0] * 26 pos = [[] for i in range(26)] for i in range(n): pos[ord(s[i]) - ord("a")].append(i) for i in range(n): x = ord(rev_s[i]) - ord("a") cur = pos[x][cnt[x]] p[i] = cur cnt[x] += 1 for i in range(n): p[i] += 1 bit = Bit(n) ans = 0 for i, x in enumerate(p): bit.add(x, 1) ans += i + 1 - bit.sum(x) print(ans)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
ans = 0 def count_inversion(arr): global ans if len(arr) <= 1: return arr mid = len(arr) // 2 left = count_inversion(arr[:mid]) right = count_inversion(arr[mid:]) final = [] i = 0 j = 0 while i < len(left) and j < len(right): if left[i] <= right[j]: final.append(left[i]) i += 1 else: final.append(right[j]) ans += len(left) - i j += 1 while i < len(left): final.append(left[i]) i += 1 while j < len(right): final.append(right[j]) j += 1 return final n = int(input()) s = input() position = [0] * n for i in range(26): temp = [] for j in range(len(s)): if s[j] == chr(ord("a") + i): temp.append(j) for j in range(len(temp)): curr = temp[j] dest = n - (temp[len(temp) - j - 1] + 1) position[curr] = dest count_inversion(position) print(ans)
ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline n = int(input()) S = input().strip() LIST = [[] for i in range(26)] for i in range(n): LIST[ord(S[i]) - 97].append(i) LEN = n + 1 BIT = [0] * (LEN + 1) def update(v, w): while v <= LEN: BIT[v] += w v += v & -v def getvalue(v): ANS = 0 while v != 0: ANS += BIT[v] v -= v & -v return ANS ANS = 0 moji = [0] * 26 for i in range(n - 1, -1, -1): s = ord(S[i]) - 97 x = LIST[s][moji[s]] ANS += x - getvalue(x + 1) moji[s] += 1 update(x + 1, 1) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline def segment_tree(ar, n): data = [0] * n + ar.copy() for i in range(n - 1, 0, -1): data[i] += data[i * 2] + data[i * 2 + 1] return data def update(data, idx, n, value): idx += n data[idx] += value while idx > 1: idx //= 2 data[idx] += value def summation(data, l, r, n): l += n r += n maxi = 0 while l < r: if l % 2 != 0: maxi += data[l] l += 1 if r % 2 != 0: r -= 1 maxi += data[r] l //= 2 r //= 2 return maxi n = int(input()) st = list(input())[:-1] ar = [] dic = {} for i in range(n): ar.append(1) if st[i] in dic: dic[st[i]].append(i) else: dic[st[i]] = [i] ar[0] = 0 for i in dic: dic[i] = dic[i][::-1] rev = st[::-1] data = segment_tree(ar, n) ans = 0 for i in range(n): cur = rev[i] ind = dic[cur].pop() ind1 = summation(data, 0, ind + 1, n) tem = ind1 - i ans += tem if tem: update(data, 0, n, 1) update(data, ind, n, -ar[ind]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()] def update(i, val): i += 1 while i <= n: BIT[i] += val i += i & -i def getsum(i): sm = 0 i += 1 while i: sm += BIT[i] i -= i & -i return sm n = inp() s = input().strip() if s == s[::-1]: print(0) exit() dt = [[] for i in range(26)] for i in range(n - 1, -1, -1): dt[ord(s[i]) - 97].append(i) BIT = [0] * (n + 1) ans = 0 for i in range(n): ind = dt[ord(s[n - i - 1]) - 97].pop(-1) put = getsum(ind) ans += ind - put update(ind, 1) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR