打卡|Uber|Minimum Removals for Valid Parenthesis
Google|Sorting A List with 3 Unique Numbers
Airbnb|First and Last Indices of an Element in a Sorted Array
Google|Create a Simple Calculator
Microsoft|Largest Product of 3 Elements
FaceBook|Find the k-th Smallest Element in a List
Apple|Maximum Profit From Stocks
Apple|Merge Overlapping Intervals
LinkedIn|Create a Balanced BST
Microsoft|Ways to Traverse a Grid
Google|Deepest Node in a Binary Tree
Recent Uber interview problem.
This question has recently emerged Uber in the interview.
You are given a string of parenthesis. Return the minimum number of parenthesis that would need to be removed in order to make the string valid. "Valid" means that each open parenthesis has a matching closed parenthesis.
Given a string of parentheses, there may be mismatches, such as a right half bracket without a left parenthesis', or two left half brackets with only one right half bracket '(()', etc. The writing algorithm returns the number of mismatched parentheses.
Test Case:
Input1:
string = "()())()"
Output1:
1
The fifth parenthesis does not match it before the parenthesis.
【Here's an ad space.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Solution:
def countInvalidParenthsis(self, string):
opened = 0
invalid = 0
for char in string:
if char == '(':
opened += 1
elif opened > 0:
opened -= 1
else:
invalid += 1
return invalid + opened
# Test Program
string = "()())()"
result = Solution().countInvalidParenthsis(string)
print(result)
# 1
If you have any suggestions for content requirements, or encountered any software, application use problems, please directly in the public number of the main interface to send a private letter to me, I think will reply in a timely manner.Thank you for your support!
Cooperation: classroom.it@hotmail.com
Hi
Hello, sir
I'm M Xiansen
This is a platform for learning together
I'll take it You want to learn
Make tutorials or write articles
We learn English together here
Learn to program algorithm
Learn all kinds of things
It's funUseful software
Remember to come back often and have a look
(^_^)a(^_^)a
Click. Read the original text Check out the Python Algorithm Live Series
Just order one if you like Share. Like it I'm watching Three companiesLet's go!
Send to the author