Reputation: 11
I would like to check if the input string contain same amount of open/close brackets. If yes print out true else false. I have wrote this code but there is some bug can someone help?
See My Code This works fine if I enter a string '()' which starts with open bracket and ends with close bracket but if I enter ')(' then it still prints out true?. Output should be:
() = true
(())=true
()) = false
(() = false
)( = false
)(() = false
etc...
Thanks for the help
EDIT:
using System;
public class Program
{
public void Main()
{
CheckParentheses ("()");
}
public void CheckParentheses (string inputParentheses){
int openParentheses = 0;
int closeParentheses = 0;
for (int i = 0; i < inputParentheses.Length; i++)
{
if (inputParentheses[i] == '(')
{
openParentheses++;
}
if (inputParentheses[i] == ')') {
closeParentheses++;
}
if (openParentheses == closeParentheses)
Console.WriteLine("true");
}
}
}
Upvotes: 1
Views: 221
Reputation: 14329
First we have to clearly understand what properties we are trying to prove about the string. This requires some time, thought, and imagination.
We could say the string must be a sentence of the language described by S → "(" S ")" S | ε
, in which case our task is to create a parser. The string is valid if and only if the parser accepts it.
Another approach is to interpret "(" as push and ")" as pop for a stack, in left-to-right order. Then, the string is valid if and only if the denoted program does not pop an empty stack and also terminates with an empty stack. We can test this just by running the program and observing the state.
Yet another approach is to interpret a string of "(" and ")" as a sequence of "1" and "-1" respectively. This is similar to the stack interpretation — the similarity can be seen through the Peano numbers. Then, the string is valid if and only if there does not exist a prefix sum less than zero and the total sum is zero.
I will explain how to implement the integer interpretation in C#. First, begin defining the function, with the string as input and our boolean answer as output.
bool AreParensBalanced(string input) { … }
First interpret the parentheses as integers.
var interpreted = input.Select(c => c == '(' ? 1 : -1);
Next find the prefix sums. Here I scoot over to an IObservable because Scan is not defined for IEnumerable — you could define Scan for IEnumerable but I'd rather leverage the existing implementation.
var prefixSums = interpreted.ToObservable().Scan(0, (a, b) => a + b).ToEnumerable()
Now find the total sum.
var totalSum = interpreted.Aggregate(0, (a, b) => a + b);
Knowing the prefix sums and the total sum, the answer can be found.
return prefixSums.All(x => x >= 0) && totalSum == 0;
Note that "there does not exist an x such that P(x) is true" is the same as "for all x, not P(x) is true", which is how I came up with All(x => x >= 0)
from the specification.
Upvotes: 0
Reputation: 1602
public static bool CheckParentheses(string inputParentheses)
{
if ( (inputParentheses.Length % 2) != 0 || inputParentheses[0] ==')' || inputParentheses[inputParentheses.Length-1] == '(')
return false;
for(int i = 0; i < inputParentheses.Length / 2; i++)
{
if(inputParentheses[i] != '(' || inputParentheses[inputParentheses.Length-i-1] != ')')
return false;
}
return true;
}
Upvotes: 0
Reputation: 216313
Instead of counting the open/close parenthesys you could check their order
public void CheckParentheses(string inputParentheses)
{
// Level counter
int parenLevel = 0;
for (int i = 0; i < inputParentheses.Length; i++)
{
// Open always good, increment the level
if (inputParentheses[i] == '(')
parenLevel++;
else if (inputParentheses[i] == ')')
parenLevel--;
// Closing good, but only if the level doesn't drop under zero
if (parenLevel < 0)
{
Console.WriteLine("false");
return;
}
}
// At the end of the loop, the level should always be zero
if(parenLevel != 0)
Console.WriteLine("false");
else
Console.WriteLine("true");
}
Upvotes: 4
Reputation: 21
If there is ever a greater number of Closed than Open, it should return a false, right? So, you can just add in the middle of your loop:
if (closeParentheses > openParentheses) {
Console.WriteLine("false");
}
Upvotes: 0