Can someone tell me how to deal with [""]?


  • 0
    B
    public class Solution {
    public List<String> removeInvalidParentheses(String s) {
    	List<String> res = new ArrayList<String>();
    	HashSet<String> set = new HashSet<String>();
        if(s == null || (!isValid(s) && s.length() < 3)) {
            return res;
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < s.length(); i++) {
            sb.append(s.charAt(i));
        }
        helper(set,sb, res, 0,s);
        return res;
    }
    private void helper(HashSet<String> set,StringBuffer sb, List<String> res, int start,String s) {
    	for(int i = start; i < s.length();i++) {
            char c = sb.charAt(i);
            sb.deleteCharAt(i);
            String str = sb.toString();
            if(isValid(str)){
            	if(!set.contains(str)) {
            		res.add(str);
            	}
            		set.add(str);
            	
            }
            sb.insert(i, c);
        }
    	return;
    }
    
    public boolean isValid(String s) {
        if(s == null || s.length() == 1) {
            return false;
        }
        if(s.length() == 0) {
            return true;
        }
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        Stack<Character> stack = new Stack<Character>();
        map.put('(',')');
        for(int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if(cur != '(' && cur != ')') {
            	continue;
            }
            if(cur == '(') {
                stack.push(cur);
            }
            else{
                if(!stack.isEmpty() && map.get(stack.peek()) == cur) {
                    stack.pop();
                }
                else return false;
            }
        }
        if( stack.isEmpty()) {
            return true;
        }
        return false;
    }
    

    }


  • 0
    Y

    Just add that string s to the result list.


  • 0
    B

    but there exist some test case like a(, or a, or (, in ( ,I can just add s, but in case a(, I should add a and remove (, how can I do that.


  • 1
    H

    Just return [""], as we can't remove any invalid parentheses from input "" so we leave it as it is


  • 0
    Y
    int len = s.length();
    
    int hasLeft = s.indexOf("(");
    
    int hasRight = s.indexOf(")");
    
    if (len <= 0 || (hasLeft == -1 && hasRight == -1)) {
    
        // Case of no parentheses.
        results.add(s);
        return new ArrayList<>(results);
    
    } else if (hasLeft == -1) {
    
        // No left parentheses.
    
        results.add(s.replace(")", ""));
        return new ArrayList<>(results);
    
    } else if (hasRight == -1) {
        // No right parentheses.
        results.add(s.replace("(", ""));
        return new ArrayList<>(results);
    }

  • 0
    B

    thx a lot~~~


  • 0
    B

    thx a lot : )


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.