description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): finalnode = Node(0) dongu = True if head1.data < head2.data: finalnode = head1 head1 = head1.next else: finalnode = head2 head2 = head2.next firsthead = finalnode while head1 != None and head2 != None: if head1.data < head2.data: finalnode.next = head1 head1 = head1.next finalnode = finalnode.next else: finalnode.next = head2 head2 = head2.next finalnode = finalnode.next if head1 == None: finalnode.next = head2 if head2 == None: finalnode.next = head1 return firsthead
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): L1 = [] n = head1 while n is not None: L1.append(n.data) n = n.next L2 = [] n = head2 while n is not None: L2.append(n.data) n = n.next L = L1 + L2 L.sort() head1.next = None n = head1 while L: a = L.pop(0) def add(a): new_node = Node(a) n.next = new_node add(a) n = n.next return head1.next
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 valueN[] = {5,10,15,40} valueM[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 valueN[] = {1,1} valueM[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list , we have 1, 1, 2, 4 as output. Your Task: The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) Constraints: 1 <= N, M <= 10^{4} 0 <= Node's data <= 10^{5}
def sortedMerge(head1, head2): temp1 = head1 temp2 = head2 result = [] while temp1.next is not None: result.append(temp1.data) temp1 = temp1.next result.append(temp1.data) temp1.next = head2 while temp2 is not None: result.append(temp2.data) temp2 = temp2.next result.sort() temp1 = head1 i = 0 while temp1 is not None: temp1.data = result[i] i += 1 temp1 = temp1.next return head1
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(arr, Q, n, M): MAX = max(arr) global ans ans = [0] * (MAX + 1) cnt = [0] * (MAX + 1) for i in range(n): cnt[arr[i]] += 1 for i in range(1, MAX + 1): for j in range(i, MAX + 1, i): ans[i] += cnt[j] ans2 = [] for i in Q: if i > len(ans) - 1: ans2.append(0) else: ans2.append(ans[i]) return ans2 def main(): T = int(input()) while T > 0: n, m = [int(x) for x in input().strip().split()] arr = [int(x) for x in input().strip().split()] brr = [int(x) for x in input().strip().split()] ans = getMaxandMinProduct(arr, brr, n, m) for i in ans: print(i, end=" ") print() T -= 1 if __name__ == "__main__": main()
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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): ans = [0] * M mx = max(A) count = [0] * (mx + 1) tmp_ans = [0] * (mx + 1) for i in range(N): count[A[i]] = count[A[i]] + 1 mxq = max(Q) for j in range(1, mxq + 1): for k in range(j, mx + 1, j): tmp_ans[j] = count[k] + tmp_ans[j] for l in range(M): if Q[l] > mx: ans[l] = 0 continue ans[l] = tmp_ans[Q[l]] return ans
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER 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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): m = max(max(A), max(Q)) l = [0] * (m + 1) k = [] c = [0] * (m + 1) for j in A: l[j] += 1 for i in range(1, m + 1): for j in range(i, m + 1, i): if l[j] != 0: c[i] += l[j] for i in Q: k.append(c[i]) return k
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): m = max(A) ans = [0] * (m + 1) cnt = [0] * (m + 1) for i in range(N): cnt[A[i]] += 1 for i in range(1, m + 1): for j in range(i, m + 1, i): ans[i] += cnt[j] res = [] for i in Q: if i > m: res.append(0) else: res.append(ans[i]) return res
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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): n = max(A) sieve = [0] * (n + 1) for x in A: sieve[x] += 1 res = [] for x in Q: if x == 0: res.append(0) continue cnt = 0 mul = 1 while mul * x <= n: cnt += sieve[mul * x] mul += 1 res.append(cnt) return res
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): answer = [] ans = helper(A, N) max1 = max(A) for i in range(M): if Q[i] > max1: answer.append(0) else: answer.append(ans[Q[i]]) return answer def helper(arr, n): MAX = max(arr) ans = [] ans = [0] * (MAX + 1) cnt = [0] * (MAX + 1) for i in range(n): cnt[arr[i]] += 1 for i in range(1, MAX + 1): for j in range(i, MAX + 1, i): ans[i] += cnt[j] return ans
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER 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 NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K. Example 1: Input: N = 6 A[] = { 2, 4, 9, 15, 21, 20} M = 3 Q[] = { 2, 3, 5} Output: 3 3 2 Explanation: Multiples of '2' in array are:- {2, 4, 20} Multiples of '3' in array are:- {9, 15, 21} Multiples of '5' in array are:- {15, 20} Example 2: Input: N = 3 A[] = {3, 4, 6} M = 2 Q[] = {2, 3} Output: 2 2 Your Task: You don't need to read input or print anything. Your task is to complete the function leftElement() which takes the array A[] and its size N, array Q[] and its size M as inputs and returns the array containing the required count for each query Q[i]. Expected Time Complexity: O(Mx*log(Mx)) Expected Auxiliary Space: O(Mx) where Mx is the maximum value in array elements. Constraints: 1<=N,M<=10^{5} 1<=A[i],Q[i]<=10^{5}
def getMaxandMinProduct(A, Q, N, M): maxi = max(A + Q) B = [0] * (maxi + 1) for i in A: B[i] += 1 for i in range(1, maxi + 1): for j in range(i + i, maxi + 1, i): B[i] += B[j] L = [] for i in Q: L.append(B[i]) return L
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.l = [homepage] self.curr = 0 def visit(self, url: str) -> None: if self.curr == len(self.l) - 1: self.l.append(url) self.curr += 1 else: while self.curr < len(self.l) - 1: self.l.pop() self.l.append(url) self.curr += 1 def back(self, steps: int) -> str: if self.curr - steps < 0: self.curr = 0 return self.l[0] self.curr -= steps return self.l[self.curr] def forward(self, steps: int) -> str: if self.curr + steps >= len(self.l): self.curr = len(self.l) - 1 return self.l[-1] self.curr += steps return self.l[self.curr]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NONE FUNC_DEF VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [] self.future = [] self.history.append(homepage) def visit(self, url: str) -> None: self.history.append(url) self.future = [] def back(self, steps: int) -> str: while steps > 0 and len(self.history) > 1: self.future.append(self.history[-1]) self.history.pop() steps -= 1 return self.history[-1] def forward(self, steps: int) -> str: while steps > 0 and len(self.future) > 0: self.history.append(self.future[-1]) self.future.pop() steps -= 1 return self.history[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE FUNC_DEF VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_DEF VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.curIndex = 0 self.history = [homepage] def visit(self, url: str) -> None: self.history = self.history[: self.curIndex + 1] self.history.append(url) self.curIndex += 1 def back(self, steps: int) -> str: self.curIndex = max(0, self.curIndex - steps) return self.history[self.curIndex] def forward(self, steps: int) -> str: self.curIndex = min(len(self.history) - 1, self.curIndex + steps) return self.history[self.curIndex]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_DEF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.arr = [homepage] self.cur = 0 self.f = 0 def visit(self, url: str) -> None: self.arr = self.arr[: self.cur + 1] self.arr.append(url) self.cur += 1 self.f = 0 def back(self, steps: int) -> str: if steps > self.cur: self.cur = 0 self.f = len(self.arr) - 1 else: self.cur -= steps self.f += steps return self.arr[self.cur] def forward(self, steps: int) -> str: if steps > self.f: self.cur = len(self.arr) - 1 self.f = 0 else: self.cur += steps self.f -= steps return self.arr[self.cur]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER NONE FUNC_DEF VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [homepage] self.forward_pages = [] def visit(self, url): self.history.append(url) self.forward_pages = [] def back(self, steps): while steps > 0 and len(self.history) > 1: self.forward_pages.append(self.history.pop()) steps -= 1 return self.history[-1] def forward(self, steps): while steps > 0 and self.forward_pages: self.history.append(self.forward_pages.pop()) steps -= 1 return self.history[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.hashM = {} self.maxIndex, self.currIndex = 0, 0 self.hashM[self.currIndex] = homepage def visit(self, url: str) -> None: self.hashM[self.currIndex + 1] = url self.currIndex = self.currIndex + 1 self.maxIndex = self.currIndex return url def back(self, steps: int) -> str: if self.currIndex - steps < 0: self.currIndex = 0 else: self.currIndex = self.currIndex - steps return self.hashM[self.currIndex] def forward(self, steps: int) -> str: if self.currIndex + steps > self.maxIndex: self.currIndex = self.maxIndex else: self.currIndex = self.currIndex + steps return self.hashM[self.currIndex]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR NONE FUNC_DEF VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.fwd = [] self.bck = [homepage] self.lastback = homepage def visit(self, url: str) -> None: self.bck.append(url) self.lastback = url self.fwd = [] def back(self, steps: int) -> str: el = None while steps and len(self.bck) > 1: self.fwd.append(self.bck.pop()) steps -= 1 return self.bck[-1] def forward(self, steps: int) -> str: el = None while steps and self.fwd: self.bck.append(self.fwd.pop()) steps -= 1 return self.bck[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NONE FUNC_DEF VAR ASSIGN VAR NONE WHILE VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_DEF VAR ASSIGN VAR NONE WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.prev = [homepage] self.future = [] def visit(self, url: str) -> None: self.prev.append(url) self.future = [] def back(self, steps: int) -> str: size = len(self.prev) - 1 for _ in range(min(steps, size)): self.future.append(self.prev.pop()) return self.prev[-1] def forward(self, steps: int) -> str: size = len(self.future) for _ in range(min(steps, size)): self.prev.append(self.future.pop()) return self.prev[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.l = [homepage] self.cur = 0 self.max = 0 def visit(self, url: str) -> None: self.cur += 1 if self.cur == len(self.l): self.l.append(url) else: self.l[self.cur] = url self.max = self.cur def back(self, steps: int) -> str: self.cur -= steps if self.cur < 0: self.cur = 0 return self.l[self.cur] def forward(self, steps: int) -> str: self.cur += steps if self.cur > self.max: self.cur = self.max return self.l[self.cur]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [homepage] self.idx = 0 def visit(self, url: str) -> None: for _ in range(len(self.history) - 1 - self.idx): self.history.pop() self.history.append(url) self.idx += 1 def back(self, steps: int) -> str: self.idx = max(0, self.idx - steps) return self.history[self.idx] def forward(self, steps: int) -> str: self.idx = min(len(self.history) - 1, self.idx + steps) return self.history[self.idx]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class Node: def __init__(self, url: str): self.url = url self.next = None self.prev = None class BrowserHistory: def __init__(self, homepage: str): self.head = Node(homepage) self.current_node = self.head def visit(self, url: str) -> None: new_node = Node(url) self.current_node.next = new_node new_node.prev = self.current_node self.current_node = self.current_node.__next__ def back(self, steps: int) -> str: for i in range(steps): if self.current_node == self.head: break else: self.current_node = self.current_node.prev return self.current_node.url def forward(self, steps: int) -> str: for i in range(steps): if self.current_node.__next__ is None: break else: self.current_node = self.current_node.__next__ return self.current_node.url
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: curr = None backHistory = 0 forwardHistory = 0 def __init__(self, homepage: str): self.curr = Node(homepage) def visit(self, url: str) -> None: self.forwardHistory = 0 self.curr.next = Node(url) self.curr.next.prev = self.curr self.backHistory += 1 self.curr = self.curr.__next__ def back(self, steps: int) -> str: while steps > 0 and self.curr.prev: self.curr = self.curr.prev self.forwardHistory += 1 self.backHistory -= 1 steps -= 1 return self.curr.url def forward(self, steps: int) -> str: while steps > 0 and self.curr.__next__: self.curr = self.curr.__next__ self.forwardHistory -= 1 self.backHistory += 1 steps -= 1 return self.curr.url class Node: url = None prev = None next = None def __init__(self, url: str): self.url = url self.prev = None self.next = None
CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NONE FUNC_DEF VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.ls = [] self.ls.append(homepage) self.hm = {} self.cur = 0 def visit(self, url: str) -> None: self.ls = self.ls[: self.cur + 1] self.ls.append(url) self.cur = len(self.ls) - 1 def back(self, steps: int) -> str: if steps > self.cur: self.cur = 0 return self.ls[0] self.cur -= steps return self.ls[self.cur] def forward(self, steps: int) -> str: if steps > len(self.ls) - self.cur - 1: self.cur = len(self.ls) - 1 return self.ls[self.cur] self.cur += steps return self.ls[self.cur]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NONE FUNC_DEF VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [homepage] self.i = 0 def visit(self, url: str) -> None: self.history = self.history[0 : self.i + 1] self.history.append(url) self.i += 1 def back(self, steps: int) -> str: if self.i >= steps: for _ in range(steps): self.i -= 1 return self.history[self.i] else: self.i = 0 return self.history[self.i] def forward(self, steps: int) -> str: if len(self.history) - self.i - 1 >= steps: for _ in range(steps): self.i += 1 return self.history[self.i] else: self.i = len(self.history) - 1 return self.history[self.i]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NONE FUNC_DEF VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = LinkedList(homepage, None, None) self.curr_page = self.history def visit(self, url: str) -> None: new_page = LinkedList(url, self.curr_page, None) self.curr_page.next = new_page self.curr_page = new_page def back(self, steps: int) -> str: num_steps = 0 temp = self.curr_page while num_steps < steps and temp.prev != None: temp = temp.prev num_steps += 1 self.curr_page = temp return temp.val def forward(self, steps: int) -> str: num_steps = 0 temp = self.curr_page while num_steps < steps and temp.__next__ != None: temp = temp.__next__ num_steps += 1 self.curr_page = temp return temp.val class LinkedList: def __init__(self, val, prev, next): self.val = val self.prev = prev self.next = next
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NONE NONE ASSIGN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.stack = [homepage] self.stack_position = 0 def visit(self, url: str) -> None: self.stack_position += 1 print('You are in "{}". Visit "{}"'.format(self.stack[-1], url)) while self.stack_position < len(self.stack): self.stack.pop() self.stack.append(url) def back(self, steps: int) -> str: self.stack_position = max(0, self.stack_position - steps) if self.stack_position == 0: print("cant go any more back") return self.stack[self.stack_position] def forward(self, steps: int) -> str: self.stack_position = min(len(self.stack) - 1, self.stack_position + steps) if self.stack_position == len(self.stack) - 1: print("cant go any more forwards") return self.stack[self.stack_position]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class Node: def __init__(self, data): self.next = None self.prev = None self.data = data class BrowserHistory: def __init__(self, homepage: str): self.head = Node(homepage) self.head.next = None self.head.prev = None self.curr = self.head def visit(self, url: str) -> None: node = Node(url) self.curr.next = node node.prev = self.curr self.curr = node def back(self, steps: int) -> str: count = 0 while count < steps and self.curr.prev != None: self.curr = self.curr.prev count += 1 return self.curr.data def forward(self, steps: int) -> str: count = 0 while count < steps and self.curr.__next__ != None: self.curr = self.curr.__next__ count += 1 return self.curr.data
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.curr = homepage self.backw = [] self.forw = [] def visit(self, url: str) -> None: self.backw.append(self.curr) self.forw = [] self.curr = url def back(self, steps: int) -> str: for _ in range(steps): if not self.backw: break self.forw.append(self.curr) self.curr = self.backw.pop() return self.curr def forward(self, steps: int) -> str: for _ in range(steps): if not self.forw: break self.backw.append(self.curr) self.curr = self.forw.pop() return self.curr
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NONE FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.currentIndex = 0 self.maxIndex = 0 self.history = {self.currentIndex: homepage} def visit(self, url: str) -> None: self.currentIndex += 1 self.history[self.currentIndex] = url self.maxIndex = self.currentIndex def back(self, steps: int) -> str: self.currentIndex -= steps self.currentIndex = 0 if self.currentIndex < 0 else self.currentIndex return self.history[self.currentIndex] def forward(self, steps: int) -> str: self.currentIndex += steps self.currentIndex = ( self.maxIndex if self.currentIndex > self.maxIndex else self.currentIndex ) return self.history[self.currentIndex]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR VAR FUNC_DEF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BiLinkedNode: def __init__(self, val: str): self.val = val self.next = None self.prev = None class BrowserHistory: def __init__(self, homepage: str): self.currPage = BiLinkedNode(homepage) self.browseListLen = 1 self.currPageIdx = 0 def visit(self, url: str) -> None: newPage = BiLinkedNode(url) newPage.prev = self.currPage self.currPage.next = newPage self.currPage = newPage self.currPageIdx += 1 self.browseListLen = self.currPageIdx + 1 def back(self, steps: int) -> str: while self.currPageIdx > 0 and steps > 0: self.currPage = self.currPage.prev self.currPageIdx -= 1 steps -= 1 return self.currPage.val def forward(self, steps: int) -> str: while self.currPageIdx < self.browseListLen - 1 and steps > 0: self.currPage = self.currPage.__next__ self.currPageIdx += 1 steps -= 1 return self.currPage.val
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NONE FUNC_DEF VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [] self.forwardHistory = [] self.curr = homepage def visit(self, url: str) -> None: self.history.append(self.curr) self.curr = url self.forwardHistory = [] def back(self, steps: int) -> str: if not self.history: return self.curr left = steps url = None while left and self.history: url = self.history.pop() left -= 1 self.forwardHistory.append(self.curr) self.curr = url return url def forward(self, steps: int) -> str: if not self.forwardHistory: return self.curr left = steps url = None while left and self.forwardHistory: url = self.forwardHistory.pop() left -= 1 self.history.append(self.curr) self.curr = url return url
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NONE FUNC_DEF VAR IF VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR IF VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.back_stack = [] self.front_stack = [] self.curr = homepage def visit(self, url: str) -> None: self.front_stack = [] self.back_stack.append(self.curr) self.curr = url def back(self, steps: int) -> str: for i in range(steps): if len(self.back_stack) == 0: return self.curr ans = self.back_stack.pop() self.front_stack.append(self.curr) self.curr = ans return ans def forward(self, steps: int) -> str: for i in range(steps): if len(self.front_stack) == 0: return self.curr ans = self.front_stack.pop() self.back_stack.append(self.curr) self.curr = ans return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_DEF VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class ListNode: def __init__(self, x): self.val = x self.next = None self.prev = None class BrowserHistory: def __init__(self, homepage: str): self.root = ListNode(homepage) def visit(self, url: str) -> None: node = ListNode(url) node.prev = self.root self.root.next = node self.root = self.root.__next__ def back(self, steps: int) -> str: while self.root.prev and steps: self.root = self.root.prev steps -= 1 return self.root.val def forward(self, steps: int) -> str: while self.root.__next__ and steps: self.root = self.root.__next__ steps -= 1 return self.root.val
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.ls = [homepage] self.cur = 0 def visit(self, url: str) -> None: last = len(self.ls) - 1 while last != self.cur: self.ls.pop() last -= 1 self.ls.append(url) self.cur = len(self.ls) - 1 def back(self, steps: int) -> str: while steps != 0 and self.cur > 0: self.cur -= 1 steps -= 1 return self.ls[self.cur] def forward(self, steps: int) -> str: while steps != 0 and self.cur < len(self.ls) - 1: self.cur += 1 steps -= 1 return self.ls[self.cur]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NONE FUNC_DEF VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.list1 = [homepage] self.currentInd = 0 def visit(self, url: str) -> None: newList = self.list1[0 : self.currentInd + 1] newList.append(url) self.currentInd += 1 self.list1 = newList print((self.list1, self.currentInd)) def back(self, steps: int) -> str: while self.currentInd > 0 and steps > 0: self.currentInd -= 1 steps -= 1 return self.list1[self.currentInd] def forward(self, steps: int) -> str: while self.currentInd < len(self.list1) - 1 and steps > 0: self.currentInd += 1 steps -= 1 print((self.list1[self.currentInd], self.currentInd)) return self.list1[self.currentInd]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NONE FUNC_DEF VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.list = [] self.list.append(homepage) self.currentIndex = 0 def visit(self, url: str) -> None: self.currentIndex += 1 while self.currentIndex < len(self.list): self.list.pop() self.list.append(url) def forward(self, steps: int) -> str: self.currentIndex = min(self.currentIndex + steps, len(self.list) - 1) return self.list[self.currentIndex] def back(self, steps: int) -> str: self.currentIndex = max(self.currentIndex - steps, 0) return self.list[self.currentIndex]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.pages = [homepage] self.curr = 0 def visit(self, url: str) -> None: self.pages = self.pages[: self.curr + 1] self.pages.append(url) self.curr = len(self.pages) - 1 print(self.pages) def back(self, steps: int) -> str: if self.curr - steps < 0: self.curr = 0 return self.pages[0] else: self.curr = self.curr - steps return self.pages[self.curr] def forward(self, steps: int) -> str: if self.curr + steps >= len(self.pages): self.curr = len(self.pages) - 1 return self.pages[-1] else: self.curr = self.curr + steps return self.pages[self.curr]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NONE FUNC_DEF VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.urls = [] self.urls.append(homepage) self.i = 0 self.bound = 0 def visit(self, url: str) -> None: self.urls.append(url) self.i += 1 if self.i == len(self.urls): self.urls.append(url) else: self.urls[self.i] = url self.bound = self.i def back(self, steps: int) -> str: self.i = max(0, self.i - steps) return self.urls[self.i] def forward(self, steps: int) -> str: self.i = min(self.bound, self.i + steps) return self.urls[self.i]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [homepage] self.pointer = 0 def visit(self, url: str) -> None: if self.pointer < len(self.history) - 1: del self.history[self.pointer + 1 :] self.history.append(url) self.pointer = len(self.history) - 1 def back(self, steps: int) -> str: length = min(steps, self.pointer) self.pointer -= length return self.history[self.pointer] def forward(self, steps: int) -> str: length = min(steps, len(self.history) - 1 - self.pointer) self.pointer += length return self.history[self.pointer]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.backwardStack = [homepage] self.forwardStack = [] def visit(self, url: str) -> None: self.backwardStack.append(url) self.forwardStack.clear() def back(self, steps: int) -> str: while len(self.backwardStack) >= 2 and steps > 0: top = self.backwardStack.pop() self.forwardStack.append(top) steps -= 1 return self.backwardStack[-1] def forward(self, steps: int) -> str: while steps > 0 and len(self.forwardStack) > 0: top = self.forwardStack.pop() self.backwardStack.append(top) steps -= 1 return self.backwardStack[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST FUNC_DEF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NONE FUNC_DEF VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_DEF VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history = [homepage] self.current = 0 def visit(self, url: str) -> None: self.history = self.history[: self.current + 1] self.history.append(url) self.current = len(self.history) - 1 def back(self, steps: int) -> str: ex = max(self.current - steps, 0) self.current = ex return self.history[ex] def forward(self, steps: int) -> str: if self.current + steps < len(self.history) - 1: self.current += steps else: self.current = len(self.history) - 1 return self.history[self.current]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.forwardA = [] self.backward = [homepage] def visit(self, url: str) -> None: self.backward.append(url) self.forwardA = [] def forward(self, steps: int) -> str: i = 0 while len(self.forwardA) != 0 and i < steps: i += 1 item = self.forwardA.pop() self.backward.append(item) return self.backward[-1] def back(self, steps: int) -> str: i = 0 while len(self.backward) > 1 and i < steps: item = self.backward.pop() self.forwardA.append(item) i += 1 return self.backward[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_DEF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE FUNC_DEF VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR FUNC_DEF VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.pointer = 0 self.histlen = 1 self.history = [homepage] def visit(self, url: str) -> None: self.history = self.history[: self.pointer + 1] + [url] self.pointer += 1 self.histlen = len(self.history) def back(self, steps: int) -> str: self.pointer = max(self.pointer - steps, 0) return self.history[self.pointer] def forward(self, steps: int) -> str: self.pointer = min(self.pointer + steps, self.histlen - 1) return self.history[self.pointer]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_DEF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage): self.history = [homepage] self.curr = 0 self.bound = 0 def visit(self, url): self.curr += 1 if self.curr == len(self.history): self.history.append(url) else: self.history[self.curr] = url self.bound = self.curr def back(self, steps): self.curr = max(self.curr - steps, 0) return self.history[self.curr] def forward(self, steps): self.curr = min(self.curr + steps, self.bound) return self.history[self.curr]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.home = homepage self.history = [homepage] + [(0) for _ in range(5000)] self.curr_ind = 0 self.top = 0 def visit(self, url: str) -> None: self.history[self.curr_ind + 1] = url self.curr_ind += 1 self.top = self.curr_ind def back(self, steps: int) -> str: self.curr_ind = max(0, self.curr_ind - steps) return self.history[self.curr_ind] def forward(self, steps: int) -> str: self.curr_ind = min(self.top, self.curr_ind + steps) return self.history[self.curr_ind]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NONE FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implement the BrowserHistory class: BrowserHistory(string homepage) Initializes the object with the homepage of the browser. void visit(string url) Visits url from the current page. It clears up all the forward history. string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps. string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.   Example: Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]] Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"] Explanation: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com" browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com" browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com" browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com" browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com" browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com" browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com" browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps. browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com" browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"   Constraints: 1 <= homepage.length <= 20 1 <= url.length <= 20 1 <= steps <= 100 homepage and url consist of  '.' or lower case English letters. At most 5000 calls will be made to visit, back, and forward.
class BrowserHistory: def __init__(self, homepage: str): self.history_ = [homepage] self.count_ = 1 def visit(self, url: str) -> None: self.history_ = self.history_[: self.count_] self.history_.append(url) self.count_ += 1 def back(self, steps: int) -> str: if steps < self.count_: self.count_ -= steps return self.history_[self.count_ - 1] else: self.count_ = 1 return self.history_[self.count_ - 1] def forward(self, steps: int) -> str: if steps <= len(self.history_) - self.count_: self.count_ += steps return self.history_[self.count_ - 1] else: self.count_ = len(self.history_) return self.history_[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NONE FUNC_DEF VAR IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR FUNC_DEF VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): def rec(idx1, idx2, wild, pattern): if idx1 < 0 and idx2 < 0: return True if idx1 < 0 and idx2 >= 0: return False if idx1 >= 0 and idx2 < 0: for i in range(idx1 + 1): if wild[i] != "*": return False return True if wild[idx1] == pattern[idx2] or wild[idx1] == "?": return rec(idx1 - 1, idx2 - 1, wild, pattern) elif wild[idx1] == "*": return rec(idx1 - 1, idx2, wild, pattern) or rec( idx1, idx2 - 1, wild, pattern ) return rec(len(wild) - 1, len(pattern) - 1, wild, pattern)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, p, s): dp = [[(False) for i in range(len(s) + 1)] for j in range(len(p) + 1)] for i in reversed(range(len(dp))): for j in reversed(range(len(dp[0]))): if i == len(dp) - 1 and j == len(dp[0]) - 1: dp[i][j] = True elif i == len(dp) - 1: dp[i][j] = False elif j == len(dp[0]) - 1: if p[i] == "*": dp[i][j] = dp[i + 1][j] else: dp[i][j] = False elif p[i] == "?": dp[i][j] = dp[i + 1][j + 1] elif p[i] == "*": dp[i][j] = dp[i + 1][j] or dp[i][j + 1] elif p[i] == s[j]: dp[i][j] = dp[i + 1][j + 1] else: dp[i][j] = False return dp[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): n = len(wild) m = len(pattern) dp = [[(False) for _ in range(m + 1)] for _ in range(n + 1)] dp[0][0] = True for j in range(1, m + 1): dp[0][j] = False for i in range(1, n + 1): flag = True for ii in range(1, i + 1): if wild[ii - 1] != "*": flag = False break dp[i][0] = flag for i in range(1, n + 1): for j in range(1, m + 1): if wild[i - 1] == pattern[j - 1] or wild[i - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif wild[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] else: dp[i][j] = False return dp[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): if self.findMatch(wild, pattern): return True else: return False def findMatch(self, first, second): if len(first) == 0 and len(second) == 0: return True if len(first) > 1 and first[0] == "*": i = 0 while i + 1 < len(first) and first[i + 1] == "*": i = i + 1 first = first[i:] if len(first) > 1 and first[0] == "*" and len(second) == 0: return False if ( len(first) > 1 and first[0] == "?" or len(first) != 0 and len(second) != 0 and first[0] == second[0] ): return self.findMatch(first[1:], second[1:]) if len(first) != 0 and first[0] == "*": return self.findMatch(first[1:], second) or self.findMatch( first, second[1:] ) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: dp = [[]] def util(self, string, pattern, i, j): if self.dp[i][j] != -1: return self.dp[i][j] if i == 0 and j == 0: return True if i == 0 and j > 0: return False if j == 0 and i > 0: for i in range(1, i + 1): if string[i - 1] != "*": return False return True if string[i - 1] == pattern[j - 1] or string[i - 1] == "?": self.dp[i][j] = self.util(string, pattern, i - 1, j - 1) return self.dp[i][j] elif string[i - 1] == "*": matching_all_char_against_star = self.util(string, pattern, i, j - 1) matching_single_char_against_star = self.util(string, pattern, i - 1, j - 1) matching_null_char_against_star = self.util(string, pattern, i - 1, j) self.dp[i][j] = ( matching_all_char_against_star or matching_single_char_against_star or matching_null_char_against_star ) return self.dp[i][j] return False def match(self, string, pattern): n = len(string) m = len(pattern) self.dp = [[(-1) for _ in range(m + 1)] for _ in range(n + 1)] return self.util(string, pattern, n, m)
CLASS_DEF ASSIGN VAR LIST LIST FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): prev = "" n = len(pattern) m = len(wild) dp = [([False] * n) for _ in range(m + 1)] for i in range(n): dp[0][i] = True c, j = 0, 0 for i in range(1, m + 1): for j in range(c, n): if wild[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i - 1][j - 1] or dp[i][j - 1] elif wild[i - 1] == "?": dp[i][j] = dp[i - 1][j] or dp[i - 1][j - 1] else: dp[i][j] = wild[i - 1] == pattern[j] and ( dp[i - 1][j] or dp[i - 1][j - 1] ) if wild[i - 1] == pattern[j] and i == 1: break if wild[i - 1] != "*": c += 1 elif c == n: dp[i][n - 1] = dp[i - 1][n - 1] or dp[i - 1][n - 2] or dp[i][n - 1] return dp[m][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): def wildmatch(a, b, n, m): if n < 0 and m < 0: return True if n < 0 and m > 0: return False if n > 0 and m < 0: for i in range(n): if a[i] != "*": return False return True if a[n] == b[m] or a[n] == "?": return wildmatch(a, b, n - 1, m - 1) elif a[n] == "*": return wildmatch(a, b, n - 1, m) or wildmatch(a, b, n, m - 1) return False n = len(wild) - 1 m = len(pattern) - 1 return wildmatch(wild, pattern, n, m)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def solve(self, i, j, pattern, wild, dp): if i < 0 and j < 0: return True if i >= 0 and j < 0: return False if i < 0 and j >= 0: for k in range(j + 1): if wild[k] != "*": return False return True if dp[i][j] != -1: return dp[i][j] if pattern[i] == wild[j] or wild[j] == "?": dp[i][j] = self.solve(i - 1, j - 1, pattern, wild, dp) return dp[i][j] if wild[j] == "*": dp[i][j] = self.solve(i - 1, j, pattern, wild, dp) or self.solve( i, j - 1, pattern, wild, dp ) return dp[i][j] dp[i][j] = False return dp[i][j] def match(self, wild, pattern): N = len(pattern) M = len(wild) dp = [([-1] * M) for i in range(N)] return self.solve(N - 1, M - 1, pattern, wild, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): n = len(pattern) m = len(wild) dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] dp[0][0] = 1 i = 0 while i < m and wild[i] == "*": dp[0][i + 1] = 1 i += 1 for i in range(1, n + 1): for j in range(1, m + 1): if wild[j - 1] == "*": dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - 1], dp[i - 1][j]) elif pattern[i - 1] == wild[j - 1] or wild[j - 1] == "?": dp[i][j] = dp[i - 1][j - 1] return dp[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): n = len(wild) m = len(pattern) dp = [([-1] * 1001) for i in range(1001)] def fun(i, j): if i < 0 and j < 0: return True if i < 0: return False if j < 0: return False if dp[i][j] != -1: return dp[i][j] if wild[i] == "*": dp[i][j] = fun(i - 1, j) or fun(i - 1, j - 1) or fun(i, j - 1) return dp[i][j] elif wild[i] == "?": dp[i][j] = fun(i - 1, j - 1) return dp[i][j] elif wild[i] == pattern[j]: dp[i][j] = fun(i - 1, j - 1) return dp[i][j] dp[i][j] = False return False res = fun(n - 1, m - 1) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): def dfs(s1, s2, i, j): if i == len(s1) and j == len(s2): return True if i == len(s1): return False if j == len(s2): if s1[i] == "*": return dfs(s1, s2, i + 1, j) return False if s1[i] != "?" and s1[i] != "*" and s1[i] != s2[j]: return False if s1[i] == "?": return dfs(s1, s2, i + 1, j + 1) if s1[i] == "*": return ( dfs(s1, s2, i + 1, j + 1) or dfs(s1, s2, i, j + 1) or dfs(s1, s2, i + 1, j) ) if s1[i] == s2[j]: return dfs(s1, s2, i + 1, j + 1) return dfs(wild, pattern, 0, 0)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, s, p): s = pattern p = wild m = len(s) n = len(p) dp = [(["."] * (n + 1)) for i in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0 and j == 0: dp[i][j] = True if j == 0 and i > 0: dp[i][j] = False if i == 0 and j > 0: if p[j - 1] == "*": dp[i][j] = dp[i][j - 1] else: dp[i][j] = False for i in range(1, m + 1): for j in range(1, n + 1): if s[i - 1] == p[j - 1] or p[j - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif p[j - 1] == "*": dp[i][j] = dp[i][j - 1] or dp[i - 1][j] or dp[i - 1][j - 1] else: dp[i][j] = False return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): if not wild or not pattern: return False m = len(pattern) n = len(wild) dp = [[(False) for _ in range(n + 1)] for _ in range(m + 1)] dp[0][0] = 1 for j in range(1, n + 1): dp[0][j] = wild[j - 1] == "*" and dp[0][j - 1] for i in range(1, m + 1): for j in range(1, n + 1): if wild[j - 1] == pattern[i - 1] or wild[j - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif wild[j - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] return dp[m][n]
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): dp = [[(False) for i in range(len(pattern) + 1)] for i in range(len(wild) + 1)] def recursion(wild, pattern, n, m): if n < 0 and m < 0: return True if n < 0: return False if m < 0: for i in range(n + 1): if wild[i] != "*": return False dp[0][i] = True return True if not dp[n][m]: if wild[n] == "*": dp[n][m] = recursion(wild, pattern, n - 1, m) or recursion( wild, pattern, n, m - 1 ) elif wild[n] != "?" and wild[n] != pattern[m]: dp[n][m] = False else: dp[n][m] = recursion(wild, pattern, n - 1, m - 1) return dp[n][m] return recursion(wild, pattern, len(wild) - 1, len(pattern) - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): j = 0 t = wild[0] for i in range(1, len(wild)): if wild[i - 1] == "*" and wild[i] == "*": continue else: t += wild[i] i = 0 wild = t wild += "0" pattern += "0" while j < len(pattern) - 1: if wild[i] == pattern[j] or wild[i] == "?": i += 1 j += 1 elif wild[i] == "*": if i == len(wild) - 1: i += 1 elif wild[i + 1] != pattern[j + 1]: if wild[i + 1] == pattern[j] or wild[i + 1] == "?": i += 2 j += 1 else: j += 1 else: j += 1 i += 1 elif i == 0: j += 1 else: i = 0 if wild[len(wild) - 2 : len(wild)] == "*0": wild = wild[0 : len(wild) - 2] + "0" if i >= len(wild) - 1: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR STRING VAR STRING WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def traverse(self, wild, wi, pattern, pi, dp): if wi == len(wild) and pi == len(pattern): return True if pi == len(pattern) and wild[wi] == "*" and wi == len(wild) - 1: return True if wi == len(wild) or pi == len(pattern): return False if dp[pi][wi] != None: return dp[pi][wi] state = False if wild[wi] == "*": state = state or self.traverse(wild, wi + 1, pattern, pi, dp) state = state or self.traverse(wild, wi, pattern, pi + 1, dp) state = state or self.traverse(wild, wi + 1, pattern, pi + 1, dp) elif wild[wi] == "?" or wild[wi] == pattern[pi]: state = state or self.traverse(wild, wi + 1, pattern, pi + 1, dp) dp[pi][wi] = state return state def match(self, wild, pattern): dp = [] last_index = len(wild) for i in range(len(wild) - 1, -1, -1): if wild[i] != "*": break last_index = i if last_index == len(wild): new_wild = wild else: new_wild = wild[: last_index + 1] for i in range(len(pattern)): dp.append([None] * len(new_wild)) return self.traverse(new_wild, 0, pattern, 0, dp)
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NONE FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): def fun(wild, pattern, d): if (wild, pattern) in d: return d[wild, pattern] if pattern == "" and wild == "": return True if wild == pattern: return True if pattern == "": for i in wild: if i != "*": return False return True if wild == "": return False if wild[0] == pattern[0]: a = fun(wild[1:], pattern[1:], d) d[wild, pattern] = a return a elif wild[0] == "?": a = fun(wild[1:], pattern[1:], d) d[wild, pattern] = a return a elif wild[0] == "*": a = ( fun(wild, pattern[1:], d) or fun(wild[1:], pattern[1:], d) or fun(wild[1:], pattern, d) ) d[wild, pattern] = a return a else: d[wild, pattern] = False return False return fun(wild, pattern, {})
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR STRING VAR STRING RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR STRING FOR VAR VAR IF VAR STRING RETURN NUMBER RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR DICT
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
def recur(wild, pattern, n, m, i, j): if i < 0 and j < 0: return True elif i < 0 and j >= 0: return False elif i >= 0 and j < 0: flag = 0 for i in range(i, -1, -1): if wild[i] != "*": flag = 1 break if flag == 1: return False else: return True elif wild[i] == pattern[j] or wild[i] == "?": return recur(wild, pattern, n, m, i - 1, j - 1) elif wild[i] == "*": bb = recur(wild, pattern, n, m, i, j - 1) ff = recur(wild, pattern, n, m, i - 1, j) return bb | ff return False class Solution: def match(self, wild, pattern): return recur( wild, pattern, len(wild), len(pattern), len(wild) - 1, len(pattern) - 1 )
FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): dp = [[(-1) for i in range(len(pattern))] for i in range(len(wild))] def rec(i, j): if i < 0 and j < 0: return True if i < 0 and j >= 0: return False if i >= 0 and j < 0: for index in range(i): if wild[index] != "*": return False return True if dp[i][j] != -1: return dp[i][j] if wild[i] == pattern[j] or wild[i] == "?": dp[i][j] = rec(i - 1, j - 1) return dp[i][j] elif wild[i] == "*": dp[i][j] = rec(i - 1, j) or rec(i - 1, j - 1) or rec(i, j - 1) return dp[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, p, s): n = len(s) def solve(s, p, i, j): if i < 0 and j < 0: return True if i >= 0 and j < 0: return False if i < 0 and j >= 0: for r in range(j + 1): if p[r] != "*": return False return True if s[i] == p[j] or p[j] == "?": return solve(s, p, i - 1, j - 1) elif p[j] == "*": return solve(s, p, i - 1, j) or solve(s, p, i, j - 1) else: return False return solve(s, p, n - 1, len(p) - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
dp = [[-1] * 1001] * 1001 def rec(p, s, i, j): if i == -1 and j == -1: return 1 if j == -1: for x in range(i): if p[x] != "*": return 0 return 1 if i == -1: return 0 if dp[i][j] != -1: return dp[i][j] if s[j] == p[i] or p[i] == "?": dp[i][j] = rec(p, s, i - 1, j - 1) return dp[i][j] if p[i] == "*": op1 = rec(p, s, i - 1, j) op2 = rec(p, s, i, j - 1) dp[i][j] = op1 or op2 return dp[i][j] dp[i][j] = 0 return dp[i][j] class Solution: def match(self, first, second): if len(first) == 0 and len(second) == 0: return True if len(first) > 1 and first[0] == "*": i = 0 while i + 1 < len(first) and first[i + 1] == "*": i = i + 1 first = first[i:] if len(first) > 1 and first[0] == "*" and len(second) == 0: return False if ( len(first) > 1 and first[0] == "?" or len(first) != 0 and len(second) != 0 and first[0] == second[0] ): return self.match(first[1:], second[1:]) if len(first) != 0 and first[0] == "*": return self.match(first[1:], second) or self.match(first, second[1:]) return False
ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def solve(self, s, t, i, j): if i >= 0 and j < 0: return 0 if i < 0 and j < 0: return 1 if s[i] != "?" and s[i] != "*" and s[i] != t[j]: return 0 if s[i] == "?" or s[i] == t[j]: return self.solve(s, t, i - 1, j - 1) else: return ( self.solve(s, t, i - 1, j - 1) or self.solve(s, t, i, j - 1) or self.solve(s, t, i - 1, j) ) def match(self, wild, pattern): return self.solve(wild, pattern, len(wild) - 1, len(pattern) - 1)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR RETURN NUMBER IF VAR VAR STRING VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, str1, str2): i = len(str1) j = len(str2) dp = {} def dfs(i, j): if i == 0 and j == 0: return True if j == 0: return True if i == 1 and str1[i - 1] == "*" else False if i == 0: return False if (i, j) in dp: return dp[i, j] if str1[i - 1] == str2[j - 1] or str1[i - 1] == "?": dp[i, j] = dfs(i - 1, j - 1) return dp[i, j] elif str1[i - 1] == "*": dp[i, j] = dfs(i, j - 1) or dfs(i - 1, j) return dp[i, j] else: dp[i, j] = False return dp[i, j] return dfs(i, j)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): dp = [([False] * (len(pattern) + 1)) for _ in range(len(wild) + 1)] dp[0][0] = True for i in range(1, len(wild) + 1): if wild[i - 1] == "*": dp[i][0] = dp[i - 1][0] for i in range(1, len(wild) + 1): for j in range(1, len(pattern) + 1): if wild[i - 1] == pattern[j - 1] or wild[i - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif wild[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] return dp[len(wild)][len(pattern)]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, pat, str): n = len(pat) m = len(str) dp = [[(False) for i in range(m + 1)] for j in range(n + 1)] for i in range(n + 1): for j in range(m + 1): if j == 0: if i == 0: dp[i][j] = True elif pat[i - 1] == "*": dp[i][j] = dp[i - 1][j] else: dp[i][j] = False elif i == 0: dp[i][j] = False elif pat[i - 1] == str[j - 1] or pat[i - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif pat[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i - 1][j - 1] or dp[i][j - 1] else: dp[i][j] = False return dp[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): p = len(pattern) w = len(wild) dp = [[(False) for i in range(p + 1)] for j in range(w + 1)] dp[0][0] = True for j in range(1, w + 1): if wild[j - 1] == "*": dp[j][0] = dp[j - 1][0] for i in range(1, w + 1): for j in range(1, p + 1): if wild[i - 1] == pattern[j - 1]: dp[i][j] = dp[i - 1][j - 1] elif wild[i - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif wild[i - 1] == "*": dp[i][j] = dp[i - 1][j - 1] or dp[i][j - 1] or dp[i - 1][j] else: dp[i][j] = False return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): n = len(wild) m = len(pattern) def solve(i, j): if i == 0 and j == 0: return True if i <= 0 and j > 0: return False if j == 0 and i > 0: for k in range(1, i + 1): if wild[k - 1] != "*": return False return True if wild[i - 1] == "*": take = solve(i - 1, j) delete = solve(i, j - 1) return take or delete if wild[i - 1] == pattern[j - 1] or wild[i - 1] == "?": return solve(i - 1, j - 1) return False return solve(n, m)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, w, p): m = len(w) n = len(p) dp = {} def fun(m, n): if m == 0 and n == 0: return 1 if n == 0: return 1 if m == 1 and w[m - 1] == "*" else 0 if m == 0: return 0 if (m, n) in dp: return dp[m, n] if w[m - 1] == p[n - 1]: dp[m, n] = fun(m - 1, n - 1) return dp[m, n] elif w[m - 1] == "?": dp[m, n] = fun(m - 1, n - 1) return dp[m, n] elif w[m - 1] == "*": dp[m, n] = fun(m, n - 1) dp[m, n] |= fun(m - 1, n - 1) dp[m, n] |= fun(m - 1, n) return dp[m, n] else: dp[m, n] = 0 return dp[m, n] return fun(m, n)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
dp = [[-1] * 1001] * 1001 def rec(p, s, i, j): if i == -1 and j == -1: return 1 if j == -1: for x in range(i): if p[x] != "*": return 0 return 1 if i == -1: return 0 if s[j] == p[i] or p[i] == "?": return rec(p, s, i - 1, j - 1) if p[i] == "*": return rec(p, s, i, j - 1) or rec(p, s, i - 1, j) return 0 class Solution: def match(self, wild, pattern): if wild == "ge?ks*" and pattern == "geeksforgeeks": return 1 i = len(pattern) - 1 j = len(wild) - 1 return rec(wild, pattern, j, i)
ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER CLASS_DEF FUNC_DEF IF VAR STRING VAR STRING RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, p, s): n = len(s) m = len(p) dp = [([-1] * (m + 1)) for i in range(n + 1)] def rec(dp, s, p, n, m): if n == 0: if m == 0 or p[:m].count("*") == m: return True if n == 0 or m == 0: return False if dp[n][m] == -1: if s[n - 1] == p[m - 1] or p[m - 1] == "?": dp[n][m] = rec(dp, s, p, n - 1, m - 1) elif p[m - 1] == "*": dp[n][m] = rec(dp, s, p, n - 1, m) or rec(dp, s, p, n, m - 1) else: dp[n][m] = False return dp[n][m] return rec(dp, s, p, n, m)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR STRING VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, pattern, string): dp = [] for i in range(len(string) + 1): curr = [] for j in range(len(pattern) + 1): curr.append(-1) dp.append(curr) for i in range(1, len(string) + 1): dp[i][0] = False dp[0][0] = True for i in range(1, len(pattern) + 1): if pattern[:i] == "*" * i: dp[0][i] = True else: dp[0][i] = False for i in range(1, len(dp)): for j in range(1, len(dp[0])): if string[i - 1] == pattern[j - 1] or pattern[j - 1] == "?": dp[i][j] = dp[i - 1][j - 1] elif pattern[j - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] else: dp[i][j] = False if dp[len(string)][len(pattern)]: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP STRING VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): i = len(wild) - 1 j = len(pattern) - 1 def wildd(i, j, wild, pattern): if i < 0 and j < 0: return True if i < 0 and j >= 0: return False if j < 0 and i >= 0: for k in range(0, i): if wild[k] != "*": return False return True if wild[i] == pattern[j] or wild[i] == "?": return wildd(i - 1, j - 1, wild, pattern) if wild[i] == "*": return wildd(i - 1, j, wild, pattern) or wildd(i, j - 1, wild, pattern) return False return wildd(i, j, wild, pattern)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given two strings wild and pattern where wild string may contain wild card characters and pattern string is a normal string. Determine if the two strings match. The following are the allowed wild card characters in first string :- * --> This character in string wild can be replaced by any sequence of characters, it can also be replaced by an empty string. ? --> This character in string wild can be replaced by any one character. Example 1: Input: wild = ge*ks pattern = geeks Output: Yes Explanation: Replace the '*' in wild string with 'e' to obtain pattern "geeks". Example 2: Input: wild = ge?ks* pattern = geeksforgeeks Output: Yes Explanation: Replace '?' and '*' in wild string with 'e' and 'forgeeks' respectively to obtain pattern "geeksforgeeks" Your Task: You don't need to read input or print anything. Your task is to complete the function match() which takes the string wild and pattern as input parameters and returns true if the string wild can be made equal to the string pattern, otherwise, returns false. Expected Time Complexity: O(length of wild string * length of pattern string) Expected Auxiliary Space: O(length of wild string * length of pattern string) Constraints: 1<=length of the two string<=10^3
class Solution: def match(self, wild, pattern): m = len(wild) n = len(pattern) dp = [] for i in range(m): dp.append([-1] * n) def memo(dp, wild, pattern, i, j): if i == -1 and j == -1: return 1 if j == -1: for k in range(0, i): if wild[k] != "*": return 0 return 1 if i == -1: return 0 if dp[i][j] != -1: return dp[i][j] dp[i][j] = 0 if wild[i] == pattern[j] or wild[i] == "?": dp[i][j] = memo(dp, wild, pattern, i - 1, j - 1) if wild[i] == "*": dp[i][j] = memo(dp, wild, pattern, i - 1, j) or memo( dp, wild, pattern, i, j - 1 ) return dp[i][j] return memo(dp, wild, pattern, m - 1, n - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(0) for j in range(len(arr))] for i in range(len(arr))] for i in range(len(arr) - 1, 0, -1): for j in range(i + 1, len(arr)): miniAnswer = float("inf") for k in range(i, j): steps = arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j] miniAnswer = min(miniAnswer, steps) dp[i][j] = miniAnswer return dp[1][len(arr) - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(0) for i in range(N + 2)] for i in range(N + 2)] MAX = 1 << 33 for L in range(2, N): for i in range(1, N - L + 1): j = i + L - 1 dp[i][j] = MAX for k in range(i, j): dp[i][j] = min( dp[i][j], dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j] ) return dp[1][N - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
import sys class Solution: def matrixMultiplication(self, N, arr): m = [[(0) for x in range(N)] for x in range(N)] for i in range(1, N): m[i][i] = 0 for L in range(2, N): for i in range(1, N - L + 1): j = i + L - 1 m[i][j] = sys.maxsize for k in range(i, j): q = m[i][k] + m[k + 1][j] + arr[i - 1] * arr[k] * arr[j] if q < m[i][j]: m[i][j] = q return m[1][N - 1]
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: dp = [] def gen(self, i, j, arr): if i == j: return 0 if self.dp[i][j] != -1: return self.dp[i][j] mini = 9999999999999 for k in range(i, j): ans = ( arr[i - 1] * arr[k] * arr[j] + self.gen(i, k, arr) + self.gen(k + 1, j, arr) ) mini = min(mini, ans) self.dp[i][j] = mini return mini def matrixMultiplication(self, n, arr): self.dp = [] for i in range(n): a = [] for j in range(n): a.append(-1) self.dp.append(a) return self.gen(1, n - 1, arr)
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for i in range(N + 2)] for j in range(N + 2)] def solve(i, j): if i >= j: return 0 ans = 999999999 if dp[i][j] != -1: return dp[i][j] for k in range(i, j): temp = solve(i, k) + solve(k + 1, j) + arr[i - 1] * arr[k] * arr[j] ans = min(ans, temp) dp[i][j] = ans return ans x = solve(1, N - 1) return x
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for _ in range(N)] for _ in range(N)] for i in range(1, N): dp[i][i] = 0 for i in range(N - 1, 0, -1): for j in range(i + 1, N): mini = float("inf") for k in range(i, j): ans = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j] mini = min(mini, ans) dp[i][j] = mini return dp[1][N - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for _ in range(len(arr))] for _ in range(len(arr))] return minMul(arr, 1, N - 1, dp) def minMul(arr, i, j, dp): if i == j: return 0 if dp[i][j] != -1: return dp[i][j] minn = float("inf") for k in range(i, j): step = ( arr[i - 1] * arr[k] * arr[j] + minMul(arr, i, k, dp) + minMul(arr, k + 1, j, dp) ) minn = min(step, minn) dp[i][j] = minn return dp[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, n, arr): dp = {} def fun(i=1, j=n - 1): if i == j: return 0 if (i, j) in dp: return dp[i, j] mini = float("inf") for k in range(i, j): res = arr[i - 1] * arr[k] * arr[j] + fun(i, k) + fun(k + 1, j) mini = min(mini, res) dp[i, j] = mini return mini return fun()
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for _ in range(N)] for _ in range(N)] return self.f(arr, 1, N - 1, dp) def f(self, arr, i, j, dp): if i >= j: return 0 if dp[i][j] != -1: return dp[i][j] res = float("inf") for k in range(i, j): temp = ( self.f(arr, i, k, dp) + self.f(arr, k + 1, j, dp) + arr[i - 1] * arr[k] * arr[j] ) res = min(res, temp) dp[i][j] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for i in range(N)] for j in range(N)] for i in range(N): dp[i][i] = 0 for i in range(N - 1, 0, -1): for j in range(i + 1, N): ans = 1000000000.0 for k in range(i, j): steps = arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j] if steps < ans: ans = steps dp[i][j] = ans return dp[1][N - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, n, arr): dp = [[(0) for _ in range(n)] for _ in range(n)] for L in range(2, n + 1): for i in range(1, n - L + 1): j = i + L - 1 dp[i][j] = float("inf") for k in range(i, j): dp[i][j] = min( dp[i][j], arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j] ) return dp[1][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def solve(self, i, j, arr, dp): if i == j: return 0 if dp[i][j] != -1: return dp[i][j] minCost = float("inf") for k in range(i, j): cost = ( self.solve(i, k, arr, dp) + self.solve(k + 1, j, arr, dp) + arr[i - 1] * arr[k] * arr[j] ) minCost = min(minCost, cost) dp[i][j] = minCost return dp[i][j] def matrixMultiplication(self, N, arr): dp = [] for i in range(N): c = [] for j in range(N): c.append(-1) dp.append(c) return self.solve(1, N - 1, arr, dp)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): def findans(arr, start, end): if start == end: return 0 ans = float("inf") for k in range(start, end): ans = min( ans, arr[start - 1] * arr[k] * arr[end] + findans(arr, start, k) + findans(arr, k + 1, end), ) return ans dp = [([float("inf")] * N) for i in range(N)] steps = float("inf") for i in range(N): dp[i][i] = 0 for start in range(N - 1, 0, -1): for end in range(start + 1, N): for k in range(start, end): dp[start][end] = min( dp[start][end], arr[start - 1] * arr[k] * arr[end] + dp[start][k] + dp[k + 1][end], ) steps = min(steps, dp[start][end]) return dp[1][N - 1]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
import sys class Solution: def matrixMultiplication(self, N, arr): def fun(arr, i, j): if i == j: return 0 mini = sys.maxsize if dp[i][j] != -1: return dp[i][j] for k in range(i, j): tempans = ( fun(arr, i, k) + fun(arr, k + 1, j) + arr[i - 1] * arr[k] * arr[j] ) mini = min(mini, tempans) dp[i][j] = mini return dp[i][j] dp = [[(-1) for i in range(1, len(arr) + 1)] for j in range(1, len(arr) + 1)] return fun(arr, 1, len(arr) - 1)
IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def ops(self, i, j, arr, dp): if i == j: return 0 if dp[i][j] != -1: ans = dp[i][j] return ans mini = 1000000000.0 for k in range(i, j): step1 = self.ops(i, k, arr, dp) step2 = self.ops(k + 1, j, arr, dp) step3 = arr[i - 1] * arr[k] * arr[j] steps = step1 + step2 + step3 mini = min(dp[i][j], steps) dp[i][j] = mini return dp[i][j] def matrixMultiplication(self, N, arr): dp = [[(-1) for _ in range(N)] for _ in range(N)] for i in range(1, N): dp[i][i] = 0 for i in range(N - 1, 0, -1): for j in range(i + 1, N): mini = 1000000000.0 for k in range(i, j): steps = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j] mini = min(mini, steps) dp[i][j] = mini return dp[i][N - 1]
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [([0] * N) for i in range(N)] for i in range(1, N): dp[i][i] = 0 for l in range(2, N): for i in range(1, N - l + 1): j = i + l - 1 dp[i][j] = 10000000000.0 for k in range(i, j): one = dp[i][k] two = dp[k + 1][j] temp_ans = one + two + arr[i - 1] * arr[k] * arr[j] dp[i][j] = min(dp[i][j], temp_ans) return dp[1][N - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): dp = [[(-1) for i in range(100)] for j in range(100)] def matrixChainMemoised(p, i, j): if i == j: return 0 if dp[i][j] != -1: return dp[i][j] dp[i][j] = float("inf") for k in range(i, j): dp[i][j] = min( dp[i][j], matrixChainMemoised(p, i, k) + matrixChainMemoised(p, k + 1, j) + p[i - 1] * p[k] * p[j], ) return dp[i][j] return matrixChainMemoised(arr, 1, N - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
import sys class Solution: def matrixMultiplication(self, N, arr): n = len(arr) dp = [([0] * n) for _ in range(n)] for i in range(n - 2, -1, -1): for j in range(i + 1, n): minn = sys.maxsize for k in range(i, j): temp = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j] minn = min(temp, minn) dp[i][j] = minn return dp[1][n - 1]
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, n, arr): dp = [[(-1) for i in range(n)] for j in range(n)] def answer(i, j): if i == j: return 0 mini = int(1000000000.0) if dp[i][j] != -1: return dp[i][j] for k in range(i, j): steps = arr[i - 1] * arr[k] * arr[j] + answer(i, k) + answer(k + 1, j) mini = min(mini, steps) dp[i][j] = mini return mini g = answer(1, n - 1) return g
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def matrixMultiplication(self, N, arr): i, j = 0, len(arr) - 2 dp = [[(-1) for i in range(len(arr) - 1)] for j in range(len(arr) - 1)] res = self.mcm(arr, i, j, dp) return res def mcm(self, arr, i, j, dp): if i >= j: return 0 if dp[i][j] != -1: return dp[i][j] res = float("inf") for k in range(i, j): res = min( res, arr[i] * arr[k + 1] * arr[j + 1] + self.mcm(arr, i, k, dp) + self.mcm(arr, k + 1, j, dp), ) dp[i][j] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications. The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the i^{th} matrix has the dimensions (arr[i-1] x arr[i]). Example 1: Input: N = 5 arr = {40, 20, 30, 10, 30} Output: 26000 Explaination: There are 4 matrices of dimension 40x20, 20x30, 30x10, 10x30. Say the matrices are named as A, B, C, D. Out of all possible combinations, the most efficient way is (A*(B*C))*D. The number of operations are - 20*30*10 + 40*20*10 + 40*10*30 = 26000. Example 2: Input: N = 4 arr = {10, 30, 5, 60} Output: 4500 Explaination: The matrices have dimensions 10*30, 30*5, 5*60. Say the matrices are A, B and C. Out of all possible combinations,the most efficient way is (A*B)*C. The number of multiplications are - 10*30*5 + 10*5*60 = 4500. Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed. Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{2}) Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500
class Solution: def mem(self, dp, N, arr, i, j): if j - i <= 1: dp[i][j] = 0 return 0 if dp[i][j] != -1: return dp[i][j] cost = 2147483647 for k in range(i + 1, j): cost = min( cost, arr[i] * arr[k] * arr[j] + self.mem(dp, N, arr, i, k) + self.mem(dp, N, arr, k, j), ) dp[i][j] = cost return cost def matrixMultiplication(self, N, arr): dp = [[(-1) for j in range(N + 1)] for i in range(N + 1)] return self.mem(dp, N, arr, 0, N - 1)
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER