leetcode 301. Remove Invalid Parentheses

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())()"]

")(" -> [""]


#include "stdafx.h"
#include<iostream>
#include<vector>
#include<set>
//#include<algorithm>
using namespace std;


/**

"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]

*/
class Solution {
	bool is_pure_letters(string s)
	{
		for (int i = 0; i < s.length(); i++)
			if (s[i] == ')' || s[i] == '(')
				return false;
		return true;
	}
	bool trim_once(vector<pair<string,int>> &substr)
	{
		bool f = false;
		set<string>container;
		vector<pair<string, int>> newsubstr;
		for (int i = 0; i < substr.size(); i++)
		{
			string tempstr = substr[i].first;
			if (is_pure_letters(tempstr))
			{
				result.push_back(tempstr);
				f = true;
				return f;
			}
			/*if (substr[i].second = substr[i].first.length())
			{
				flag = true;
				result.push_back(substr[i].first);
				continue;
			}*/
			int count(0);
			for (int j = substr[i].second; j < substr[i].first.length(); j++)
			{
				if (tempstr[j] == '(')
					count++;
				if (tempstr[j] == ')')
				{
					count--;
					if (count < 0)
					{
						for (int k = j; k >= 0; k--)
						{
							if (tempstr[k] == ')')
							{
								string newstr = tempstr;
								newstr.erase(newstr.begin()+k);
								if (container.find(newstr) == container.end())
								{
									container.insert(newstr);
									newsubstr.push_back(pair<string, int>(newstr, j));
									
								}
							}
						}
						break;
					}
				}

				if (j == tempstr.length() - 1)
				{
					if (count == 0)
					{
						f = true;
						result.push_back(tempstr);
					}
					else if (count > 0)
					{
						for (int k = 0; k < tempstr.length(); k++)
						{
							if (tempstr[k] == '(')
							{
								string newstr = tempstr; 
								newstr.erase(newstr.begin() + k);
								if (container.find(newstr) == container.end())
								{
									container.insert(newstr);
									newsubstr.push_back(pair<string, int>(newstr, 0));
								}
							}
						}
					}
				}
			}
			if (substr[i].second == substr[i].first.length())
			{
				f = true;
				result.push_back(substr[i].first);
			}
		}
		substr = newsubstr;
		//for (int i = 0; i < substr.size();)
		return f;
	}
	int mintime;
	vector<string>result;
public:
	vector<string> removeInvalidParentheses(string s) {
		
		int count(0),times(0);
		mintime = s.length();
		set<string>returnvalue;
		vector<pair<string,int>>strs;
		strs.push_back(pair<string,int>(s,0));
		bool flag = trim_once(strs);
		while (!flag)
		{
			flag = trim_once(strs);
		}
		return result;
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	Solution sl;
	vector<string>re = sl.removeInvalidParentheses(string(")()))())))"));
	for (int i = 0; i < re.size(); i++)
		cout << re[i].c_str() << endl;
	system("pause");
	return 0;
}

acceped


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值