Table of Contents 0:00 Problem Statement 0:38 Solution 6:54 Pseudo Code 9:27 Code - Python 10:12 Code - C++
@mathematics3398
9 күн бұрын
class Solution: def maxLength(self, str): st = [] ans = 0 st.append(-1) for i in range(len(str)): if str[i] == '(': st.append(i) else: if len(st) != 0: st.pop() if len(st) != 0: ans = max(ans, i - st[-1]) else: st.append(i) return ans
@mathematics3398
9 күн бұрын
class Solution { public: int maxLength(string str) { stack st; int ans = 0; st.push(-1); for (int i = 0; i < str.length(); i++) { if (str[i] == '(') { st.push(i); } else { if (!st.empty()) st.pop(); if (!st.empty()) ans = max(ans, i - st.top()); else st.push(i); } } return ans; } };
Пікірлер: 3