patterncsharpModerate
Matching parentheses
Viewed 0 times
parenthesesmatchingstackoverflow
Problem
I have a string that contains a random order of parenthesis
Example:
Here, the second iteration over the dictionary has \$O(n)\$ complexity. Can I improve its time complexity?
{[()]}. I want to check if for any given parenthesis is there a matching closing one.Example:
}}}{{{ //true
{[] //false
{[()]} //trueprivate static bool checkIfWellFormatted(string inputString)
{
if (string.IsNullOrEmpty(inputString))
throw new ArgumentException("String is empty");
if ((inputString.Length % 2) != 0)
return false;
Dictionary _inputDictionary = new Dictionary();
foreach (Char c in inputString)
{
if (!_inputDictionary.ContainsKey(c))
_inputDictionary.Add(c, 0);
else
_inputDictionary[c] += 1;
}
foreach (var item in _inputDictionary)
{
char oppKey = '\0';
if (item.Key == '{')
oppKey = '}';
if (item.Key == '}')
oppKey = '{';
if (item.Key == '(')
oppKey = ')';
if (item.Key == ')')
oppKey = '(';
if (item.Key == '[')
oppKey = ']';
if (item.Key == ']')
oppKey = '[';
if (_inputDictionary.ContainsKey(oppKey))
{
var value = _inputDictionary[oppKey];
if (value != item.Value)
return false;
}
else
return false;
}
return true;
}Here, the second iteration over the dictionary has \$O(n)\$ complexity. Can I improve its time complexity?
Solution
You could evict the count dictionary altogether and use three counters. Next, you scan each character and increment/decrement the corresponding counter. The answer is
All in all, I had this in mind:
Also, from mathematical perspective, I would accept the empty string as a valid "parenthezation."
true if after scanning all the characters all three counters are zero. For example, if we scan [, we increment the counter for [ and ]; if we scan ], we decrement that very same counter. (Before the scan all counters are set to zero.) All in all, I had this in mind:
private static bool WellFormatted(string inputString)
{
int braceCount = 0; // Counts '{' and '}'.
int parenthesisCount = 0; // Counts '(' and ')'.
int squareBracketCount = 0; // Counts '[' and ']'.
foreach (char c in inputString)
{
switch (c)
{
case '{':
braceCount++;
break;
case '}':
braceCount--;
break;
case '(':
parenthesisCount++;
break;
case ')':
parenthesisCount--;
break;
case '[':
squareBracketCount++;
break;
case ']':
squareBracketCount--;
break;
}
}
return braceCount == 0
&& parenthesisCount == 0
&& squareBracketCount == 0;
}Also, from mathematical perspective, I would accept the empty string as a valid "parenthezation."
Code Snippets
private static bool WellFormatted(string inputString)
{
int braceCount = 0; // Counts '{' and '}'.
int parenthesisCount = 0; // Counts '(' and ')'.
int squareBracketCount = 0; // Counts '[' and ']'.
foreach (char c in inputString)
{
switch (c)
{
case '{':
braceCount++;
break;
case '}':
braceCount--;
break;
case '(':
parenthesisCount++;
break;
case ')':
parenthesisCount--;
break;
case '[':
squareBracketCount++;
break;
case ']':
squareBracketCount--;
break;
}
}
return braceCount == 0
&& parenthesisCount == 0
&& squareBracketCount == 0;
}Context
StackExchange Code Review Q#155034, answer score: 11
Revisions (0)
No revisions yet.