DETECT DUPLICATE PARENTHESES IN AN EXPRESSION
namespace DuplicateParanthesesInExpression
{
class Program
{
public static Boolean IsDupParanthesesPresent(String Expression)
{
if (Expression == null || Expression.Length == 0)
return false;
Stack<char> S = new Stack<char>();
for (int i = 0; i < Expression.Length; i++)
{
if (Expression[i] == '(') S.Push(Expression[i]);
//If you find '(' in stack when you are find ')' in the input ,
//it means there's a duplicate parantheses in the expression.
else if (Expression[i] == ')')
{
if (!(S.Peek() == '('))
{
while (S.Count > 0 && S.Pop() != '(') ;
}
else return true;
}
else if (!char.IsLetter(Expression[i])) S.Push(Expression[i]);
}
return false;
}
static void Main(string[] args)
{
String Expression = "((a+b)*(c+d))";
Console.WriteLine(IsDupParanthesesPresent(Expression));
}
}
}
This fails for your input. It returns false
ReplyDeletesalomon台灣
ReplyDelete