I want to be able to get string
and check if the Parentheses
are valid.
For example:
"(ew)[]" - this will be valid.
"(ew[)]" - this will be not valid.
This is what I have tried:
public static bool CheckString(string input)
{
int braceSum = 0, squareSum = 0, parenSum = 0;
foreach (char c in input)
{
if (c == '{')
braceSum++;
if (c == '}')
braceSum--;
if (c == '[')
squareSum++;
if (c == ']')
squareSum--;
if (c == '(')
parenSum++;
if (c == ')')
parenSum--;
//check for negatives (pair closes before it opens)
if (braceSum < 0 || squareSum < 0 || parenSum < 0)
return false;
}
return (braceSum == 0 && squareSum == 0 && parenSum == 0);
}
So in both cases my code will return true
. Do you have any suggestions about what I need to add in order for the program to work correctly?