description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities). It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. Input The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n). Output Output the maximum possible profit. Examples Input 4 1 2 2 3 3 4 Output 1 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 Output 0 Input 6 1 2 2 3 2 4 5 4 6 4 Output 4
def put(): return map(int, input().split()) def length(i, p, u, v): vis[i] = 1 max_i, max_here_i, sec_max_here_i = 0, 0, 0 for j in tree[i]: if j != p and (i, j) not in [(u, v), (v, u)]: max_j, max_here_j = length(j, i, u, v) if max_here_j >= max_here_i: sec_max_here_i = max_here_i max_here_i = max_here_j elif max_here_j > sec_max_here_i: sec_max_here_i = max_here_j max_i = max(max_i, max_j) max_i = max(max_i, max_here_i + sec_max_here_i + 1) return max_i, max_here_i + 1 n = int(input()) tree = [[] for _ in range(n)] vis = [0] * n edge = [] for _ in range(n - 1): x, y = put() x, y = x - 1, y - 1 edge.append((x, y)) tree[x].append(y) tree[y].append(x) ans = 0 for u, v in edge: l1, _ = length(u, -1, u, v) l2, _ = length(v, -1, u, v) ans = max((l1 - 1) * (l2 - 1), ans) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities). It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. Input The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n). Output Output the maximum possible profit. Examples Input 4 1 2 2 3 3 4 Output 1 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 Output 0 Input 6 1 2 2 3 2 4 5 4 6 4 Output 4
__author__ = "Darren" def solve(): def get_diameter(u): depth, v = dfs(u, set()) return dfs(v, set())[0] def dfs(u, visited): visited.add(u) max_depth, deepest_node = -1, u for v in adj_list[u]: if v not in visited: depth, w = dfs(v, visited) if depth > max_depth: max_depth, deepest_node = depth, w return max_depth + 1, deepest_node n = int(input()) roads = [] adj_list = [set() for _i in range(n + 1)] for _i in range(n - 1): u, v = map(int, input().split()) roads.append((u, v)) adj_list[u].add(v) adj_list[v].add(u) ans = 0 for u, v in roads: adj_list[u].remove(v) adj_list[v].remove(u) ans = max(ans, get_diameter(u) * get_diameter(v)) adj_list[u].add(v) adj_list[v].add(u) print(ans) solve()
ASSIGN VAR STRING FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities). It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. Input The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n). Output Output the maximum possible profit. Examples Input 4 1 2 2 3 3 4 Output 1 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 Output 0 Input 6 1 2 2 3 2 4 5 4 6 4 Output 4
def dfs(v): for u in graph[v]: if dist[u] == -1: dist[u] = dist[v] + 1 dfs(u) n = int(input()) graph = [[] for i in range(n)] edges = [] for i in range(n - 1): u, v = map(int, input().split()) edges.append((u - 1, v - 1)) graph[u - 1].append(v - 1) graph[v - 1].append(u - 1) ans = 0 for e in edges: graph[e[0]].remove(e[1]) graph[e[1]].remove(e[0]) dist = [-1] * n dist[e[0]] = 0 dfs(e[0]) far = e[0] for v in range(n): if dist[far] < dist[v]: far = v dist = [-1] * n dist[far] = 0 dfs(far) prod = max(dist) dist = [-1] * n dist[e[1]] = 0 dfs(e[1]) far = e[1] for v in range(n): if dist[far] < dist[v]: far = v dist = [-1] * n dist[far] = 0 dfs(far) prod = prod * max(dist) ans = max(ans, prod) graph[e[0]].append(e[1]) graph[e[1]].append(e[0]) print(ans)
FUNC_DEF FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities). It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. Input The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n). Output Output the maximum possible profit. Examples Input 4 1 2 2 3 3 4 Output 1 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 Output 0 Input 6 1 2 2 3 2 4 5 4 6 4 Output 4
n = int(input()) p = [[] for i in range(n + 1)] for j in range(n - 1): a, b = map(int, input().split()) p[a].append(b) p[b].append(a) def g(b, c): x = y = d = 0 for a in p[b]: if a != c: s, z = g(a, b) z, y, x = sorted([x, y, z]) d = max(d, s) return max(d, x + y), x + 1 print(max(g(a, b)[0] * g(b, a)[0] for a in range(n + 1) for b in p[a] if b > a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): i = 0 if not s: return True m, n = len(t), len(s) for ch in t: if ch == s[i]: if i == n - 1: return True i += 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): length = len(s) idx = 0 for i in range(length): idx = t.find(s[i], idx) if idx == -1: return False idx += 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): t = iter(t) return all(c in t for c in s) def isSubsequenceFei(self, s, t): p, l = 0, len(s) if l == p: return True for c_t in t: if s[p] == c_t: p += 1 if p == l: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): start = 0 for i in range(len(s)): index = t[start:].find(s[i]) print(index) if index == -1: return False else: index += start start = index + 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): if len(s) > len(t): return False for i in s: if i in t: index = t.find(i) t = t[index + 1 :] else: return False return True
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): if s == "": return True s_num = 0 temp = s[s_num] for i in t: if i == temp: s_num = s_num + 1 if s_num >= len(s): return True temp = s[s_num] return False
CLASS_DEF FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): if len(s) == 0: return True if len(s) > len(t): return False if s[0] not in t: return False else: return self.isSubsequence(s[1:], t[t.index(s[0]) + 1 :])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not). Example 1: s = "abc", t = "ahbgdc" Return true. Example 2: s = "axc", t = "ahbgdc" Return false. Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code? Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
class Solution: def isSubsequence(self, s, t): index = 0 ans = True for i in s: if i not in t[index:]: ans = False break else: index += t[index:].index(i) + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): one, two = 1, 1 for i in range(n - 1): temp = one one = one + two two = temp return one % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def __init__(self): self.h = {} def countWays(self, n): if n == 0: return 1 if n < 0: return 0 elif self.h.get(n, -1) != -1: return self.h[n] self.h[n] = self.countWays(n - 1) + self.countWays(n - 2) return self.h[n] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): ways = [-1] * (n + 1) return self.totalWays(n, ways, n) def totalWays(self, i, ways, n): if i == 0: return 1 elif i < 0: return 0 elif ways[i] != -1: return ways[i] else: m = 10**9 + 7 ways[i] = ( self.totalWays(i - 1, ways, n) % m + self.totalWays(i - 2, ways, n) % m ) % m return ways[i]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): ans = 1 prev1 = 1 prev2 = 1 for i in range(2, n + 1): ans = prev1 + prev2 prev2 = prev1 prev1 = ans return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): one = 1 two = 1 mod = 1000000007 for i in range(2, n + 1): two, one = one + two, two return two % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n == 0 or n == 1: return 1 else: arr = [1] * (n + 1) for i in range(2, n + 1): arr[i] = arr[i - 1] + arr[i - 2] return arr[-1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
mod = 10**9 + 7 class Solution: def countWays(self, n): if n is 1: return 1 prev2 = 1 prev1 = 2 for i in range(2, n): cur = (prev2 + prev1) % mod prev2 = prev1 prev1 = cur return prev1
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def rec(self, i, dp): if i == -1: return 0 if i == 0: return 1 if dp[i] != -1: return dp[i] dp[i] = (self.rec(i - 1, dp) + self.rec(i - 2, dp)) % (10**9 + 7) return dp[i] def countWays(self, n): dp = [-1] * (n + 1) return self.rec(n, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): a = 1 b = 2 res = 0 if n == 1 or n == 2: return n while n > 2: n -= 1 res = (a + b) % (10**9 + 7) a = b b = res return res % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): mod = 10**9 + 7 arr = [1, 1] for i in range(2, n + 1): ele = (arr[i - 1] % mod + arr[i - 2] % mod) % mod arr.append(ele) return arr[n]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n == 1 or n == 2: return 1 if n == 1 else 2 mod = 10**9 + 7 ans = [(0) for _ in range(n)] ans[0] = 1 ans[1] = 2 for i in range(2, n): ans[i] = ans[i - 1] + ans[i - 2] return ans[n - 1] % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [] ans = 0 for i in range(n + 1): if i == 0 or i == 1: dp.append(1) continue dp.append(dp[i - 1] + dp[i - 2]) return dp[-1] % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [] mod = 1000000000 + 7 dp.append(1) for i in range(1, n): dp.append((dp[i - 1] % mod + dp[i - 2] % mod) % mod) return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
mod = 10**9 + 7 class Solution: def helper(self, n, i, dp): if i == n: return 1 if i > n: return 0 if dp[i + 1] == -1: l = self.helper(n, i + 1, dp) dp[i + 1] = l else: l = dp[i + 1] if dp[i + 2] == -1: k = self.helper(n, i + 2, dp) dp[i + 2] = l else: k = dp[i + 2] ans = k + l dp[i] = ans return ans def countWays(self, n): dp = [(-1) for _ in range(n + 2)] ans = self.helper(n, 0, dp) % mod return ans
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n == 1: return 1 if n == 2: return 2 dp = [] dp.append(1) dp.append(2) for i in range(2, n): dp.append(dp[i - 1] + dp[i - 2]) return dp[-1] % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): cache = {} def count(i): if i in cache: return cache[i] if i <= 2: return i res = count(i - 1) + count(i - 2) cache[i] = res return res return count(n) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): _m = 10**9 + 7 if n <= 1: return n _p1 = 1 _p2 = 1 for _ in range(2, n + 1): _c = _p1 + _p2 _p2 = _p1 _p1 = _c return _p1 % _m
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): req = n - 1 prev, cur = 1, 1 tot = 2 if n == 1: return 1 if n == 2: return 2 for i in range(req - 2): newstep = prev + cur prev = cur cur = newstep tot += newstep tot = tot % (pow(10, 9) + 7) return tot + 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN BIN_OP VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def sol(self, idx, dp): m = 1000000007 if idx <= 1: dp[idx] = 1 if dp[idx] != -1: return dp[idx] dp[idx] = (self.sol(idx - 1, dp) + self.sol(idx - 2, dp)) % m return dp[idx] def countWays(self, n): dp = [-1] * (n + 1) return self.sol(n, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def driver(self, n): if not self.table[n - 1] == -1: return self.table[n - 1] else: ans = self.driver(n - 1) + self.driver(n - 2) self.table[n - 1] = ans return ans def countWays(self, n): self.table = [-1] * n if n > 0: self.table[0] = 1 if n > 1: self.table[1] = 2 return int(self.driver(n)) % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n == 1: return 1 prev2 = 1 prev1 = 2 for i in range(n - 2): s = prev1 + prev2 prev2 = prev1 prev1 = s return prev1 % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [(-1) for i in range(n + 1)] dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2] return dp[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n in [1, 2]: return n a, b = 1, 2 for i in range(3, n + 1): temp = (a + b) % (10**9 + 7) a, b = b, temp return temp
CLASS_DEF FUNC_DEF IF VAR LIST NUMBER NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [-1] * (n + 1) return self.solve(n, dp) def solve(self, n, dp): if n == 0: return 1 if n < 0: return 0 if dp[n] != -1: return dp[n] dp[n] = self.solve(n - 1, dp) + self.solve(n - 2, dp) return dp[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): l = [(0) for x in range(n + 1)] l[1] = 1 if n > 1: l[1], l[2] = 1, 2 for i in range(3, n + 1): l[i] = (l[i - 1] + l[i - 2]) % (10**9 + 7) return l[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
mod = 1000000000.0 + 7 class Solution: def countWays(self, n): prev2 = 1 prev1 = 1 for i in range(2, n + 1): cur = (prev2 + prev1) % mod prev2 = prev1 prev1 = cur return int(prev1)
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def recCountWays(self, index, dp): if index <= 1: return 1 if dp[index] != -1: return dp[index] downOneIndex = self.recCountWays(index - 1, dp) downTwoIndex = 0 if index > 1: downTwoIndex = self.recCountWays(index - 2, dp) dp[index] = (downOneIndex + downTwoIndex) % (10**9 + 7) return (downOneIndex + downTwoIndex) % (10**9 + 7) def countWays(self, n): prev1 = 1 prev = 1 for index in range(n + 1): downOneIndex = prev downTwoIndex = 0 if index > 1: downTwoIndex = prev1 curr = (downOneIndex + downTwoIndex) % (10**9 + 7) prev1 = prev prev = curr return prev
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [-1] * (n + 1) dp[n] = 1 dp[n - 1] = 1 for i in range(n - 2, -1, -1): dp[i] = dp[i + 1] + dp[i + 2] return dp[0] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): l = [1, 2] for i in range(2, n): l.append(l[i - 1] + l[i - 2]) return l[n - 1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: c = 0 dp = [-1] * 10000 def gen(self, n, count): if count == n: return 1 if count == n - 1: return 1 if count < n and self.dp[count] != -1: return self.dp[count] l = self.gen(n, count + 1) r = self.gen(n, count + 2) self.dp[count] = l + r return l + r def countWays(self, n): if n == 1: return 1 self.dp = [-1] * 10000 self.gen(n, 0) suma = 0 return self.dp[0] % (10**9 + 7)
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def mul(self, A, B, MOD): K = len(A) C = [[(0) for i in range(K)] for j in range(K)] for i in range(1, K): for j in range(1, K): for k in range(1, K): C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD return C def power(self, A, n): if n == 1: return A if n % 2 != 0: A = self.mul(A, self.power(A, n - 1), 1000000007) else: A = self.power(A, n // 2) A = self.mul(A, A, 1000000007) return A def countWays(self, n): F = [(0) for i in range(3)] F[1] = 1 F[2] = 2 K = 2 MOD = 1000000007 C = [[(0) for i in range(K + 1)] for j in range(K + 1)] for i in range(1, K): C[i][i + 1] = 1 C[K][1] = 1 C[K][2] = 1 if n <= 2: return F[n] C = self.power(C, n - 1) result = 0 for i in range(1, K + 1): result = (result + C[1][i] * F[i]) % MOD return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): mod = 1000000007 dp = [-1] * (n + 5) def solve(n, dp): if n <= 2: dp[n] = n return dp[n] if dp[n] != -1: return dp[n] dp[n] = (solve(n - 1, dp) % mod + solve(n - 2, dp) % mod) % mod return dp[n] return solve(n, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): mod = 1000000007 if n == 1: return 1 if n == 2: return 2 s1, s2 = 1, 2 for i in range(3, n + 1): s3 = s1 + s2 s2, s1 = s3, s2 return s3 % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def fun(self, n, dp): if n == 0: return 1 if n < 0: return 0 if dp[n] != -1: return dp[n] oneStep = self.fun(n - 1, dp) twoStep = self.fun(n - 2, dp) dp[n] = (oneStep + twoStep) % (10**9 + 7) return dp[n] def countWays(self, n): dp = [(-1) for i in range(n + 1)] return self.fun(n, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
mod = 10**9 + 7 class Solution: def countWays(self, n): dp = [-1] * (n + 1) def memo(n): if n == 0 or n == 1: return 1 if dp[n] != -1: return dp[n] dp[n] = (memo(n - 1) + memo(n - 2)) % mod return dp[n] % mod return memo(n)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): a = 0 b = 1 for i in range(n): a, b = b, (a + b) % (10**9 + 7) return b
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def solve(self, n, dp): if n < 0: return 0 if dp[n] != -1: return dp[n] if n >= 2: dp[n] = self.solve(n - 1, dp) + self.solve(n - 2, dp) return dp[n] def countWays(self, n): if n <= 2 and n >= 0: return n dp = [(-1) for i in range(n + 1)] dp[1] = 1 dp[2] = 2 a = self.solve(n, dp) m = 1000000007 return dp[n] % m
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def recCountWays(self, index, dp): if index <= 1: return 1 if dp[index] != -1: return dp[index] downOneIndex = self.recCountWays(index - 1, dp) downTwoIndex = 0 if index > 1: downTwoIndex = self.recCountWays(index - 2, dp) dp[index] = (downOneIndex + downTwoIndex) % (10**9 + 7) return (downOneIndex + downTwoIndex) % (10**9 + 7) def countWays(self, n): table = [(0) for i in range(n + 1)] table[0] = table[1] = 1 for index in range(1, n + 1): downOneIndex = table[index - 1] downTwoIndex = 0 if index > 1: downTwoIndex = table[index - 2] table[index] = (downOneIndex + downTwoIndex) % (10**9 + 7) return table[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): dp = [-1] * (n + 1) return self.ways(n, dp) def ways(self, n, dp): if n <= 1: return 1 if dp[n] != -1: return dp[n] val = (self.ways(n - 1, dp) + self.ways(n - 2, dp)) % (10**9 + 7) dp[n] = val return val
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n < 1: return 1 count = [(0) for _ in range(n + 1)] count[0] = count[1] = 1 for stair in range(2, n + 1): for step in range(1, 3): count[stair] += count[stair - step] count[stair] %= 1000000007 return count[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
import sys sys.setrecursionlimit(10**4) class Solution: def countWays(self, n): i = 0 count = 0 flag = 0 mem = n * [-1] return self.helper(n, i, count, mem) % (10**9 + 7) def helper(self, n, i, count, mem): if i == n: count = count + 1 return count elif i > n: return 0 if mem[i] != -1: return mem[i] else: sum = ( self.helper(n, i + 1, count, mem) + self.helper(n, i + 2, count, mem) ) % (10**9 + 7) mem[i] = sum return sum
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n <= 2: return n step1 = 1 step2 = 2 div = 10**9 + 7 for i in range(n - 2): ans = (step1 + step2) % div step1 = step2 step2 = ans return step2
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): f = 1 s = 2 if n == 1: return f m = 10**9 + 7 for i in range(3, n + 1): cur = (s % m + f % m) % m f = s s = cur return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): d = {} def dfs(n): if n not in d: if n <= 1: d[n] = 1 else: d[n] = (dfs(n - 1) + dfs(n - 2)) % (10**9 + 7) return d[n] return dfs(n)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def __init__(self): self.MODULAR_CONSTANT = 10**9 + 7 def countWays(self, n): dp = {} dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = (dp[i - 1] + dp[i - 2]) % self.MODULAR_CONSTANT return dp[n] % self.MODULAR_CONSTANT def helper(self, dp, n): if n == 0 or n == 1: dp[n] = 1 return dp[n] % self.MODULAR_CONSTANT elif n in dp: return dp[n] % self.MODULAR_CONSTANT else: dp[n] = ( self.helper(dp, n - 1) + self.helper(dp, n - 2) ) % self.MODULAR_CONSTANT return dp[n] % self.MODULAR_CONSTANT
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): prev2 = 0 prev1 = 1 for i in range(1, n + 1): cur = (prev1 + prev2) % (10**9 + 7) prev2 = prev1 prev1 = cur return prev1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): fib1 = 1 fib2 = 2 if n == 1: return 1 if n == 2: return 2 total = 0 for ctr in range(2, n): total = fib1 + fib2 fib1 = fib2 fib2 = total return total % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def sol(self, n): m = 1000000007 prev2 = 1 prev = 1 for i in range(2, n + 1): curr = (prev + prev2) % m prev2 = prev prev = curr return prev def countWays(self, n): return self.sol(n)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): if n == 0 or n == 1: return 1 dp = [(0) for i in range(n + 1)] dp[1] = 1 dp[0] = 1 for i in range(1, n + 1): dp[i] = int( (dp[i - 1] % (1000000000.0 + 7) + dp[i - 2] % (1000000000.0 + 7)) % (1000000000.0 + 7) ) return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
mod = 10**9 + 7 class Solution: def helper(self, dp, n): if n == 2: return 2 elif n == 1: return 1 if n in dp: return dp[n] else: dp[n] = (self.helper(dp, n - 1) + self.helper(dp, n - 2)) % mod return dp[n] def countWays(self, n): dp = dict() return self.helper(dp, n)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): def fun(n): mod = 10**9 + 7 if n == 0: return 1 if n == 1: return 1 if arr[n] != -1: return arr[n] % mod else: arr[n] = (fun(n - 1) % mod + fun(n - 2) % mod) % mod return arr[n] arr = [] for i in range(n + 1): arr.append(-1) return fun(n)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def sol(self, dp, n): m = 1000000007 dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = (dp[i - 1] + dp[i - 2]) % m return dp[len(dp) - 1] def countWays(self, n): dp = [-1] * (n + 1) return self.sol(dp, n)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def countWays(self, n): first, second = 1, 1 for i in range(2, n + 1): first, second = second, first + second return second % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a time. Way 2: Climb 1 stair at a time. Way 3: Climb 2 stairs, then 1 stair and then 1 stair. Way 4: Climb 1 stair, then 2 stairs then 1 stair. Way 5: Climb 1 stair, then 1 stair and then 2 stairs. Example 2: Input: n = 10 Output: 89 Explanation: There are 89 ways to reach the 10th stair. Your Task: Complete the function countWays() which takes the top stair number m as input parameters and returns the answer % 10^9+7. Expected Time Complexity : O(n) Expected Auxiliary Space: O(1) Constraints: 1 ≤ n ≤ 10^{4}
class Solution: def recur(self, n, cache): if n in cache: return cache[n] if n == 0 or n == 1: cache[n] = 1 return cache[n] cache[n] = self.recur(n - 1, cache) + self.recur(n - 2, cache) return cache[n] def countWays(self, n): dp = [(0) for _ in range(n + 1)] dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2] return dp[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N >= K + W - 1: return 1 if N < K: return 0 if K == 0: return 1 probs = [0] * (W + 2) probs[0] = 1 probs[1] = 1 / W for i in range(1, K): probs[(i + 1) % (W + 2)] = ( probs[i % (W + 2)] * (W + 1) / W - probs[(i + 2) % (W + 2)] / W ) for i in range(W): probs[(K + i + 1) % (W + 2)] = ( probs[(K + i) % (W + 2)] - probs[(K + i + 2) % (W + 2)] / W ) if K % (W + 2) < (N + 1) % (W + 2): return sum(probs[K % (W + 2) : (N + 1) % (W + 2)]) else: return sum(probs[K % (W + 2) :]) + sum(probs[: (N + 1) % (W + 2)])
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 dp = [0] * (W + K) dp[0] = 1 sliding_sum = 1 for i in range(1, K + W): dp[i] = sliding_sum / W if i < K: sliding_sum += dp[i] if i - W >= 0: sliding_sum -= dp[i - W] dp[i - W] = 0 return sum(dp[K : N + 1]) / sum(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: dp = [0] * (N + W) for i in range(K, N + 1): dp[i] = 1 S = min(W, N - K + 1) for i in range(K - 1, -1, -1): dp[i] = S / W S += dp[i] - dp[i + W] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 running_sum = 1 P = [1] for i in range(1, W): P.append(1 / W * running_sum) running_sum += P[-1] for i in range(W, K): P.append(1 / W * running_sum) running_sum += P[-1] - P[i - W] res = 0 running_sum = sum(P[max(0, K - W) : K]) print( sum(P[2:12]) + sum(P[3:12]) + sum(P[4:12]) + sum(P[5:12]) + sum(P[6:12]) + sum(P[7:12]) + sum(P[8:12]) + sum(P[9:12]) + sum(P[10:12]) + sum(P[11:12]) ) for i in range(max(0, K - W), K): res += P[i] / W * max(min(i + W, N) - max(i + 1, K) + 1, 0) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN VAR VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution1: def new21Game(self, N: int, K: int, W: int) -> float: dp = [None] * (K + W) s = 0 for i in range(K, K + W): dp[i] = 1 if i <= N else 0 s += dp[i] for i in range(K - 1, -1, -1): dp[i] = s / W s = s - dp[i + W] + dp[i] return dp[0] class Solution: def new21Game(self, N: int, K: int, W: int) -> float: dp = [(1 if i <= N else 0) for i in range(K + W)] s = sum(dp[K : K + W]) for i in range(K - 1, -1, -1): dp[i] = s / W s = s - dp[i + W] + dp[i] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N > K + W - 1 or K == 0: return 1 dp = [0] * (K + W) dp[1] = 1 / W for i in range(2, K + 1): if i == W + 1: dp[i] = dp[i - 1] + (dp[W] - 1) / W else: dp[i] = ((W + 1) * dp[i - 1] - (dp[i - 1 - W] if i >= W + 1 else 0)) / W for i in range(K + 1, K + W): dp[i] = dp[i - 1] - (dp[i - W - 1] if i - W >= 2 else 0) / W if i == W + 1: dp[i] -= 1 / W res = sum(dp[i] for i in range(K, N + 1)) / sum(dp[i] for i in range(K, K + W)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1.0 if K - 1 + W <= N: return 1.0 prob = [0.0] * (K + W) avg = 0 for i in range(W): prob[K + i] = float(K + i <= N) avg += prob[K + i] / W for i in range(K)[::-1]: prob[i] += avg avg += (prob[i] - prob[i + W]) / W return prob[0]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: dp = [0.0] * (N + W + 1) for k in range(K, N + 1): dp[k] = 1.0 S = sum(dp[i] for i in range(K, K + W + 1)) for k in reversed(list(range(K))): dp[k] = S / float(W) S += dp[k] - dp[k + W] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 dp = [0] cur, temp = 1, 1 / W for i in range(1, W + 1): dp.append(cur / W) if i < K: cur += dp[-1] total = sum(dp[1 : min(W + 1, K)]) left = 1 for i in range(W + 1, W + K): dp.append(total / W) total -= dp[left] left += 1 if i < K: total += dp[-1] end = sum(dp[K:]) good_end = sum(dp[K : N + 1]) return good_end / end
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities. Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points? Example 1: Input: N = 10, K = 1, W = 10 Output: 1.00000 Explanation: Alice gets a single card, then stops. Example 2: Input: N = 6, K = 1, W = 10 Output: 0.60000 Explanation: Alice gets a single card, then stops. In 6 out of W = 10 possibilities, she is at or below N = 6 points. Example 3: Input: N = 21, K = 17, W = 10 Output: 0.73278 Note: 0 <= K <= N <= 10000 1 <= W <= 10000 Answers will be accepted as correct if they are within 10^-5 of the correct answer. The judging time limit has been reduced for this question.
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N < K: return 0 if N > K - 1 + W: return 1 dp = [0] * (K + W) for k in range(K, N + 1): dp[k] = 1 S = N - K + 1 for k in range(K - 1, -1, -1): dp[k] = S / W S += dp[k] - dp[k + W] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def zeta(s): n = len(s) z = [0] * n l, r = 0, 0 for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z s = input() n = len(s) z = zeta(s) l = [0] * n for i in z: l[i] += 1 cum = [l[-1]] for i in range(n - 2, -1, -1): cum.append(l[i] + cum[-1]) cum = cum[::-1] ans = [(n, 1)] for i in range(n - 1, 0, -1): if i + z[i] == n: ans.append((n - i, cum[n - i] + 1)) print(len(ans)) for i in sorted(ans): print(*i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
import sys input = sys.stdin.readline s = input().rstrip() n = len(s) i = 1 j = 0 a = [0] * n a[0] = n while i < n: while i + j < n and s[j] == s[i + j]: j += 1 a[i] = j if j == 0: i += 1 continue k = 1 while i + k < n and k + a[k] < j: a[i + k] = a[k] k += 1 i += k j -= k ans = [0] * (n + 1) for aa in a: ans[aa] += 1 for i in range(n)[::-1]: ans[i] += ans[i + 1] ans2 = [] for i in range(1, n + 1): if a[n - i] == i: ans2.append([i, ans[i]]) print(len(ans2)) for x in ans2: print(*x)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = input() n = len(s) z = [0] * n l = 0 r = 0 for i in range(1, n): if s[i] == s[0]: if i < r: j = i + min(z[i - l], r - i) while j < n and s[j] == s[j - i]: j += 1 z[i] = j - i if j > r: l = i r = j else: l = i r = i + 1 while r < n and s[r] == s[r - l]: r += 1 z[i] = r - l else: z[i] = 0 a = [] c = [0] * n for i in range(1, n): if i + z[i] == n: a.append(z[i]) c[z[i]] += 1 for i in reversed(range(n - 1)): c[i] += c[i + 1] print(len(a) + 1) for x in sorted(a): print(x, c[x] + 1) print(n, 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def Count(s, t): res = 0 for i in range(len(s) - len(t) + 1): if s[i : i + len(t)] == t: res += 1 return res s = input() n = len(s) p = [0] * n z = [0] * n ans = [0] * n for i in range(1, n): p[i] = p[i - 1] while p[i] > 0 and s[i] != s[p[i]]: p[i] = p[p[i] - 1] if s[i] == s[p[i]]: p[i] += 1 l = r = 0 for i in range(1, n): if i <= r: z[i] = min(z[i - l], r - i + 1) while i + z[i] < n and s[i + z[i]] == s[z[i]]: z[i] += 1 if i + z[i] - 1 > r: l, r = i, i + z[i] - 1 for i in range(n): if z[i] > 0: ans[z[i] - 1] += 1 for i in range(n - 2, -1, -1): ans[i] += ans[i + 1] output = [] for i in range(n): if z[n - i - 1] == i + 1: output.append((i, ans[i])) print(len(output) + 1) for i in range(len(output)): print(output[i][0] + 1, output[i][1] + 1) print(n, 1)
FUNC_DEF ASSIGN VAR NUMBER 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 VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def z_advanced(s): Z = [0] * len(s) Z[0] = len(s) rt = 0 lt = 0 for k in range(1, len(s)): if k > rt: n = 0 while n + k < len(s) and s[n] == s[n + k]: n += 1 Z[k] = n if n > 0: lt = k rt = k + n - 1 else: p = k - lt right_part_len = rt - k + 1 if Z[p] < right_part_len: Z[k] = Z[p] else: i = rt + 1 while i < len(s) and s[i] == s[i - k]: i += 1 Z[k] = i - k lt = k rt = i - 1 return Z def kmptab(s): tab = [0] * len(s) i = 1 j = 0 while i < len(s): if s[i] == s[j]: tab[i] = j + 1 i += 1 j += 1 elif j != 0: j = tab[j - 1] else: i += 1 return tab def __starting_point(): s = input() tab = kmptab(s) my_set = set() i = len(s) while i != 0: my_set.add(i) i = tab[i - 1] V = [] dict = {} for i in my_set: V.append(i) dict[i] = 0 Z = z_advanced(s) v = [] V.sort() my_tab = [0] * (len(s) + 1) for i in Z: my_tab[i] += 1 somme = 0 for i in range(len(my_tab) - 1, -1, -1): my_tab[i] += somme somme = my_tab[i] for i in dict: dict[i] = my_tab[i] v.append((dict[i], i)) v.sort(key=lambda tup: tup[1]) print(len(v)) for i in v: print(str(i[1]) + " " + str(i[0])) __starting_point()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
dep = dict() def kmp(st): tam = len(st) pi = [0] * tam for i in range(1, tam): j = pi[i - 1] while j > 0 and st[j] != st[i]: j = pi[j - 1] if st[j] == st[i]: pi[i] = j + 1 dep[pi[i]] = pi[pi[i] - 1] return pi s = input() arr = kmp(s) points = [0] * len(s) toimp = list() actpi = arr[-1] while actpi != 0: toimp.append(actpi) actpi = arr[actpi - 1] toimp.sort() for i in range(len(arr)): points[arr[i]] += 1 for i in range(len(s) - 1, 0, -1): if i in dep: points[dep[i]] += points[i] print(len(toimp) + 1) for i in toimp: print(str(i) + " " + str(points[i] + 1)) print(str(len(s)) + " 1")
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def preZ(s): n = len(s) z = [0] * n z[0] = n r = 0 if n == 1: return z while r + 1 < n and s[r] == s[r + 1]: r += 1 z[1] = r l = 1 if r > 0 else 0 for k in range(2, n): bl = r + 1 - k gl = z[k - l] if gl < bl: z[k] = z[k - l] else: j = max(0, r - k + 1) while k + j < n and s[j] == s[k + j]: j += 1 z[k] = j l, r = k, k + j - 1 return z def f(s): n = len(s) c = [0] * (n + 1) b = [False] * (n + 1) z = preZ(s) for i in range(n): t = z[i] b[t] = b[t] or z[i] + i == n c[t] += 1 for t in range(n - 1, 0, -1): c[t] += c[t + 1] return "\n".join( ["%d" % sum(b)] + [("%d %d" % (t, c[t])) for t in range(1, n + 1) if b[t]] ) print(f(input()))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL STRING BIN_OP LIST BIN_OP STRING FUNC_CALL VAR VAR BIN_OP STRING VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def getLPS(s2, l): lps = [0] * l i = 1 j = 0 while i < l: if s2[i] == s2[j]: lps[i] = j + 1 j += 1 i += 1 elif j != 0: j = lps[j - 1] else: lps[i] = 0 i += 1 return lps s = input() n = len(s) p = getLPS(s, n) ans = [] f = [0] * n for i in range(n): f[p[i]] += 1 for i in range(n - 1, 0, -1): f[p[i - 1]] += f[i] pos = n - 1 ans = [[n, 1]] while pos > 0: if p[pos] > 1: ans.append([p[pos], f[p[pos]] + 1]) pos = p[pos] - 1 if len(s) > 1: f = s.count(s[0]) if s[0] == s[len(s) - 1]: ans.append([1, f]) print(len(ans)) for i in sorted(ans): print(i[0], i[1])
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
from itertools import accumulate def zfunc(s): n = len(s) z = [0] * n left = right = 0 for i in range(1, n): if i <= right: z[i] = min(z[i - left], right - i + 1) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > right: left, right = i, i + z[i] - 1 return z s = input() ans = set() z = zfunc(s) z[0] = len(s) res = [0] * (len(s) + 1) for i in z: res[i] += 1 res = [*accumulate(res[::-1])][::-1] for i, j in enumerate(z[::-1]): if j > i: ans.add((j, res[j])) print(len(ans)) for i in sorted([*ans]): print(*i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = " " + input() n = len(s) r, c = [-1] * n, [1] * n for i in range(1, n): r[i] = r[i - 1] + 1 while r[i] and s[r[i]] != s[i]: r[i] = r[r[i] - 1] + 1 d, n = [], n - 1 for i in range(n, 1, -1): c[r[i]] += c[i] while n > 0: d.append(str(n) + " " + str(c[n])) n = r[n] print(len(d)) d.reverse() print("\n".join(d))
ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = input() n = len(s) p = [0] * n k = 0 for i in range(1, n): while k != 0 and s[k] != s[i]: k = p[k - 1] if s[k] == s[i]: k += 1 p[i] = k a = [] k = n while k != 0: a += [k] k = p[k - 1] c = [0] * (n + 1) for i in range(n): c[p[i]] += 1 for i in range(n - 1, 1, -1): c[p[i - 1]] += c[i] print(len(a)) for t in reversed(a): print(t, c[t] + 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER VAR LIST VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = input() n = len(s) if n == 1: print(1) print(1, 1) quit() priya = [-1] * n q = -1 for i in range(1, n): while q >= 0 and s[i] != s[q + 1]: q = priya[q] if s[i] == s[q + 1]: q += 1 priya[i] = q cnt = [1] * n for i in range(n - 1, -1, -1): if priya[i] >= 0: cnt[priya[i]] += cnt[i] Ans = [(n, 1)] q = priya[n - 1] while q >= 0: Ans.append((q + 1, cnt[q])) q = priya[q] Ans.sort() print(len(Ans)) for i in Ans: print(*i)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def computeLPSArray(pat, M, lps): len = 0 lps[0] i = 1 while i < M: if pat[i] == pat[len]: len += 1 lps[i] = len i += 1 elif len != 0: len = lps[len - 1] else: lps[i] = 0 i += 1 return lps def count_subs_occ(s, m, l): occ = [0] * (m + 1) for i in range(m): occ[l[i]] += 1 for i in range(m - 1, 0, -1): occ[l[i - 1]] += occ[i] for i in range(m + 1): occ[i] += 1 return occ s = input() m = len(s) l = [0] * m x = computeLPSArray(s, m, l) x = [0] + x y = count_subs_occ(s, m, l) a = x[-1] if a == 0: print(1) print(m, 1) elif a == 1: print(2) print(1, y[a]) print(m, 1) else: u = x[-1] v = x[u] ans = [[u, y[u]]] while 1: if v == 0: break u = v v = x[u] temp = [u, y[u]] ans.append(temp) ans.sort() print(len(ans) + 1) for i in range(len(ans)): print(ans[i][0], ans[i][1]) print(m, 1)
FUNC_DEF ASSIGN VAR NUMBER EXPR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST LIST VAR VAR VAR WHILE NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = str(input()) lps = [0] * 100005 dp = [0] * 100005 ada = [0] * 100005 tunda = [0] * 100005 n = len(s) i = 1 j = 0 lps[0] = 0 while i < n: if s[i] == s[j]: j += 1 lps[i] = j i += 1 elif j == 0: lps[i] = 0 i += 1 else: j = lps[j - 1] for i in range(n - 1, -1, -1): tunda[i] += 1 dp[lps[i]] += tunda[i] if lps[i]: tunda[lps[i] - 1] += tunda[i] j = n vector = [] while 1: vector.append((j, 1 + dp[j])) j = lps[j - 1] if j == 0: break vector.reverse() print(len(vector)) for i in vector: print(i[0], i[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = input() n = len(s) p = [0] * (n + 1) ans = [0] * (n + 1) j = 0 li = [n] for i in range(1, n): while j > 0 and s[i] != s[j]: j = p[j - 1] if s[i] == s[j]: j += 1 p[i] = j for i in range(0, n): ans[p[i]] += 1 for i in range(n - 1, -1, -1): ans[p[i - 1]] += ans[i] for i in range(0, n + 1): ans[i] += 1 j = n - 1 while j > 0: if p[j]: li.append(p[j]) j = p[j] - 1 li.reverse() print(len(li)) for i in range(0, len(li)): print(li[i], ans[li[i]])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def compute_z(data): z = [(0) for _ in range(len(data))] z[0] = len(data) l = 0 r = 0 for i in range(1, len(data)): if i <= r: z[i] = min(z[i - l], r - i + 1) while i + z[i] < len(data) and data[z[i]] == data[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: r = i + z[i] - 1 l = i return z data = input() n = len(data) z = compute_z(data) feq_z = [(0) for _ in range(n)] for value in z: if value == 0: continue index = value - 1 feq_z[index] += 1 for i in range(n - 2, -1, -1): feq_z[i] += feq_z[i + 1] count = 0 for i in range(n - 1, -1, -1): if z[i] == n - i: count += 1 print(count) for i in range(n - 1, -1, -1): if z[i] == n - i: print("{} {}".format(z[i], feq_z[z[i] - 1]))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = str(input()) n = len(s) z = [0] li = [-1] d = dict() ans = set() lis = [] def zf(s): l = r = 0 for i in range(1, n): z.append(0) if i <= r: z[-1] = min(z[i - l], r - i + 1) while i + z[i] < n and s[i + z[i]] == s[z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 li.append(z[i]) if z[i] + i == n: ans.add(z[i]) zf(s) li.sort() x = 1 for i in range(1, len(li)): if li[i] == li[i - 1]: x += 1 else: d.update({li[i - 1]: x}) x = 1 d.update({li[-1]: x}) x = len(li) + 1 for i in d: x -= d[i] d[i] += x print(len(ans) + 1) for i in ans: lis.append((i, d[i])) lis.sort() for i in lis: print(i[0], i[1]) print(n, 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
from itertools import accumulate def z_algorithm(s): n = len(s) l, d = 1, 0 ans = [0] * n ans[0] = n while l < n: while l + d < n and s[d] == s[l + d]: d += 1 ans[l] = d if d == 0: l += 1 continue k = 1 while l + k < n and k + ans[k] < d: ans[l + k] = ans[k] k += 1 l += k d -= k return ans z = z_algorithm(input()) n = len(z) ct = [0] * (n + 1) match = [False] * (n + 1) for i, v in enumerate(z): ct[v] += 1 match[n - i] = n - i == v for i in range(n - 1, -1, -1): ct[i] += ct[i + 1] buf = [] for i, v in enumerate(ct): if i > 0 and v > 0 and match[i]: buf.append("{} {}".format(i, v)) print(len(buf)) print("\n".join(buf))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def prefix_function(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j > 0 and s[j] != s[i]: j = pi[j - 1] pi[i] = j + (s[j] == s[i]) return pi def solve(s): n = len(s) pi = prefix_function(s) j = n prefixes = [] while j: prefixes.append(j) j = pi[j - 1] print(len(prefixes)) if len(prefixes) == 1: print(n, 1) return cnt = [0] * (n + 1) for i in range(n): cnt[pi[i]] += 1 for i in range(n - 1, 0, -1): cnt[pi[i - 1]] += cnt[i] for i in range(n + 1): cnt[i] += 1 for i in reversed(prefixes): print(i, cnt[i]) def main(): s = input() solve(s) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
s = input() n = len(s) m = [0] * (n + 1) z = [0] * (n + 1) l = 0 r = 0 li = [n] for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 m[z[i]] += 1 if i + z[i] == n: li.append(z[i]) for i in range(n - 1, 0, -1): m[i] += m[i + 1] li.reverse() print(len(li)) for i in range(0, len(li)): print(li[i], m[li[i]] + 1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|]. Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring. -----Input----- The single line contains a sequence of characters s_1s_2...s_{|}s| (1 ≤ |s| ≤ 10^5) — string s. The string only consists of uppercase English letters. -----Output----- In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers l_{i} c_{i}. Numbers l_{i} c_{i} mean that the prefix of the length l_{i} matches the suffix of length l_{i} and occurs in string s as a substring c_{i} times. Print pairs l_{i} c_{i} in the order of increasing l_{i}. -----Examples----- Input ABACABA Output 3 1 4 3 2 7 1 Input AAA Output 3 1 3 2 2 3 1
def calculate_z_array(s): n = len(s) z = [0] * n z[0] = n l = 0 r = 0 for i in range(1, n): if i >= l and i <= r: z[i] = min(z[i - l], r - i + 1) while i + z[i] < n and s[z[i]] == s[z[i] + i]: z[i] += 1 if i + z[i] - 1 < n and i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z s = input() n = len(s) z = calculate_z_array(s) no_of_prefix_suffix = 0 is_prefix_sufix = [False] * (n + 1) for i in range(n): if i + z[i] == n: no_of_prefix_suffix += 1 is_prefix_sufix[z[i]] = True cnt = [0] * (n + 1) for i in range(n): cnt[z[i]] += 1 for i in range(n - 1, -1, -1): cnt[i] += cnt[i + 1] print(no_of_prefix_suffix) for i in range(1, n + 1): if is_prefix_sufix[i] == True: print(i, " ", cnt[i])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR VAR
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string $s$, removes it from $s$ and prepends (adds to the beginning) it to their own string. The game ends when the string $s$ becomes empty. The winner is the player with a lexicographically smaller string. If the players' strings are equal, then it's a draw. A string $a$ is lexicographically smaller than a string $b$ if there exists such position $i$ that $a_j = b_j$ for all $j < i$ and $a_i < b_i$. What is the result of the game if both players play optimally (e. g. both players try to win; if they can't, then try to draw)? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Each testcase consists of a single line — a non-empty string $s$, consisting of lowercase Latin letters. The length of the string $s$ is even. The total length of the strings over all testcases doesn't exceed $2000$. -----Output----- For each testcase, print the result of the game if both players play optimally. If Alice wins, print "Alice". If Bob wins, print "Bob". If it's a draw, print "Draw". -----Examples----- Input 2 forces abba Output Alice Draw -----Note----- One of the possible games Alice and Bob can play in the first testcase: Alice picks the first letter in $s$: $s=$"orces", $a=$"f", $b=$""; Bob picks the last letter in $s$: $s=$"orce", $a=$"f", $b=$"s"; Alice picks the last letter in $s$: $s=$"orc", $a=$"ef", $b=$"s"; Bob picks the first letter in $s$: $s=$"rc", $a=$"ef", $b=$"os"; Alice picks the last letter in $s$: $s=$"r", $a=$"cef", $b=$"os"; Bob picks the remaining letter in $s$: $s=$"", $a=$"cef", $b=$"ros". Alice wins because "cef" < "ros". Neither of the players follows any strategy in this particular example game, so it doesn't show that Alice wins if both play optimally.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): s = input()[:-1] n = len(s) l, r = 0, n - 1 while l < r: if s[l] == s[r]: l += 1 r -= 1 else: break s = s[l : r + 1] res = "Draw" for i in range(1, len(s), 2): if s[i] != s[i - 1]: res = "Alice" break print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string $s$, removes it from $s$ and prepends (adds to the beginning) it to their own string. The game ends when the string $s$ becomes empty. The winner is the player with a lexicographically smaller string. If the players' strings are equal, then it's a draw. A string $a$ is lexicographically smaller than a string $b$ if there exists such position $i$ that $a_j = b_j$ for all $j < i$ and $a_i < b_i$. What is the result of the game if both players play optimally (e. g. both players try to win; if they can't, then try to draw)? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Each testcase consists of a single line — a non-empty string $s$, consisting of lowercase Latin letters. The length of the string $s$ is even. The total length of the strings over all testcases doesn't exceed $2000$. -----Output----- For each testcase, print the result of the game if both players play optimally. If Alice wins, print "Alice". If Bob wins, print "Bob". If it's a draw, print "Draw". -----Examples----- Input 2 forces abba Output Alice Draw -----Note----- One of the possible games Alice and Bob can play in the first testcase: Alice picks the first letter in $s$: $s=$"orces", $a=$"f", $b=$""; Bob picks the last letter in $s$: $s=$"orce", $a=$"f", $b=$"s"; Alice picks the last letter in $s$: $s=$"orc", $a=$"ef", $b=$"s"; Bob picks the first letter in $s$: $s=$"rc", $a=$"ef", $b=$"os"; Alice picks the last letter in $s$: $s=$"r", $a=$"cef", $b=$"os"; Bob picks the remaining letter in $s$: $s=$"", $a=$"cef", $b=$"ros". Alice wins because "cef" < "ros". Neither of the players follows any strategy in this particular example game, so it doesn't show that Alice wins if both play optimally.
for _ in range(int(input())): s = input() l = 0 r = len(s) - 1 c = 0 while l < r and s[l] == s[r]: l += 1 r -= 1 for i in range(l, r, 2): if s[i] == s[i + 1]: continue c = 1 if c == 0: print("Draw") else: print("Alice")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string $s$, removes it from $s$ and prepends (adds to the beginning) it to their own string. The game ends when the string $s$ becomes empty. The winner is the player with a lexicographically smaller string. If the players' strings are equal, then it's a draw. A string $a$ is lexicographically smaller than a string $b$ if there exists such position $i$ that $a_j = b_j$ for all $j < i$ and $a_i < b_i$. What is the result of the game if both players play optimally (e. g. both players try to win; if they can't, then try to draw)? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Each testcase consists of a single line — a non-empty string $s$, consisting of lowercase Latin letters. The length of the string $s$ is even. The total length of the strings over all testcases doesn't exceed $2000$. -----Output----- For each testcase, print the result of the game if both players play optimally. If Alice wins, print "Alice". If Bob wins, print "Bob". If it's a draw, print "Draw". -----Examples----- Input 2 forces abba Output Alice Draw -----Note----- One of the possible games Alice and Bob can play in the first testcase: Alice picks the first letter in $s$: $s=$"orces", $a=$"f", $b=$""; Bob picks the last letter in $s$: $s=$"orce", $a=$"f", $b=$"s"; Alice picks the last letter in $s$: $s=$"orc", $a=$"ef", $b=$"s"; Bob picks the first letter in $s$: $s=$"rc", $a=$"ef", $b=$"os"; Alice picks the last letter in $s$: $s=$"r", $a=$"cef", $b=$"os"; Bob picks the remaining letter in $s$: $s=$"", $a=$"cef", $b=$"ros". Alice wins because "cef" < "ros". Neither of the players follows any strategy in this particular example game, so it doesn't show that Alice wins if both play optimally.
for _ in range(int(input())): n = input() while n and n[-1] == n[0]: n = n[1:-1] x = 1 for i in range(0, len(n), 2): x *= n[i] == n[i + 1] print(["Alice", "Draw"][x])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST STRING STRING VAR