301.Remove Invalid Parentheses

Source: Internet
Author: User

Topic:

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note:the input string may contain letters other than the parentheses ( and ) .

Examples:

"() ()) ()", ["() () () ()", "(()) ()", "(a) ()) ()", "(a) () ()", "(a) () ()", "(A) (") ["]

Links: http://leetcode.com/problems/remove-invalid-parentheses/

Exercises

Given a string, remove invalid brackets and output all result sets. The first thought is DFS + backtracking, did not persist. Later in the discuss found the Jeantimex great God's BFs method is very good, so moved to learn from. The method is that each time we remove a "(" or ")" and then add the new string to the queue and continue with the calculation. Note that you need to set a Boolean foundresult, and if we find the result at this level, we will not proceed to the following for loop. It should also be possible to continue pruning, such as recording the length of the current result Len, we do not validate isvalid this step when the remaining string length in the queue is smaller than this Len.

Time Complexity-o (n * 2n), Space complexity-o (2n)

 Public classSolution { PublicList<string>removeinvalidparentheses (String s) {List<String> res =NewArraylist<>(); if(s = =NULL) {            returnRes; } Queue<String> queue =NewLinkedlist<>(); Set<String> visited =NewHashset<>();        Queue.offer (s); BooleanFoundresult =false;  while(!Queue.isempty ()) {s=Queue.poll (); if(IsValid (s)) {Res.add (s); Foundresult=true; }            if(foundresult) {Continue; }             for(inti = 0; I < s.length (); i++) {                Charc =S.charat (i); if(c = = ' (' | | c = = ') ') {String T= s.substring (0, I) + s.substring (i + 1); if(!visited.contains (t))                        {Queue.offer (t);                    Visited.add (t); }                }            }        }                returnRes; }        Private BooleanIsValid (String s) {intLeftcount = 0;  for(inti = 0; I < s.length (); i++) {            Charc =S.charat (i); if(c = = ' (') {Leftcount++; } Else if(c = = ') ') {Leftcount--; }            if(Leftcount < 0) {                return false; }        }                returnLeftcount = = 0; }}

Reference:

Https://leetcode.com/discuss/67842/share-my-java-bfs-solution

Https://leetcode.com/discuss/67853/my-c-dfs-solution-16ms

Https://leetcode.com/discuss/67919/java-optimized-dfs-solution-3-ms

Https://leetcode.com/discuss/67861/short-python-bfs

Https://leetcode.com/discuss/72208/easiest-9ms-java-solution

Https://leetcode.com/discuss/67861/short-python-bfs

Https://leetcode.com/discuss/72208/easiest-9ms-java-solution

Https://leetcode.com/discuss/67908/java-bfs-solution-16ms-avoid-generating-duplicate-strings

Https://leetcode.com/discuss/67821/and-bfs-java-solutions-add-more-optimized-fast-dfs-solution

Https://leetcode.com/discuss/68038/clean-java-solution-bfs-optimization-40ms

Https://leetcode.com/discuss/68010/fast-optimized-dfs-java-solution

301.Remove Invalid Parentheses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.