Skip to content

Added BalancedParenthesesChecker.cs, NextGreaterElement.cs and ReverseStack.cs #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 3, 2024
56 changes: 56 additions & 0 deletions DataStructures/Stack/BalancedParenthesesChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
namespace DataStructures.Stack
{
/// <summary>
/// It checks if an expression has matching and balanced parentheses.
/// @author Mohit Singh
/// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
/// </summary>
public class BalancedParenthesesChecker

Check warning on line 9 in DataStructures/Stack/BalancedParenthesesChecker.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

DataStructures/Stack/BalancedParenthesesChecker.cs#L9

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
private static readonly Dictionary<char, char> ParenthesesMap = new Dictionary<char, char>()

Check notice on line 11 in DataStructures/Stack/BalancedParenthesesChecker.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

DataStructures/Stack/BalancedParenthesesChecker.cs#L11

Remove these redundant parentheses.

Check failure on line 11 in DataStructures/Stack/BalancedParenthesesChecker.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Dictionary<,>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in DataStructures/Stack/BalancedParenthesesChecker.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Dictionary<,>' could not be found (are you missing a using directive or an assembly reference?)
{
{ '(', ')' },
{ '{', '}' },
{ '[', ']' },
};
/// <summary>
/// This method checks if an expression has matching and balanced parentheses.
/// </summary>
/// <param name="expression">string containing parenthesis</param>
/// <returns>Boolean value</returns>
public static bool IsBalanced(string expression)
{
Stack<char> stack = new Stack<char>();
foreach (char c in expression)
{
if (c == '(' || c == '{' || c == '[')
{
stack.Push(c);
}
else if (c == ')' || c == '}' || c == ']')
{
if (stack.Count == 0)
{
return false;
}
char open = stack.Pop();

if (!IsMatchingPair(open, c))
{
return false;
}
}
else
{
//since there are no other brackets, this is unreachable code
}
}
return stack.Count == 0;
}
private static bool IsMatchingPair(char open, char close)
{
return ParenthesesMap.ContainsKey(open) && ParenthesesMap[open] == close;
}
}
}
33 changes: 33 additions & 0 deletions DataStructures/Stack/NextGreaterElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
namespace DataStructures.Stack
{
/// <summary>
/// For each element in an array, the utility finds the next greater element on the right side using a stack.
/// @author Mohit Singh
/// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
/// </summary>
public class NextGreaterElement

Check warning on line 9 in DataStructures/Stack/NextGreaterElement.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

DataStructures/Stack/NextGreaterElement.cs#L9

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
/// <summary>
/// For each element in an array, this method finds the next greater element on the right side using a stack.
/// </summary>
/// <param name="nums">Integer Array for which NextGreaterElement needs to be computed</param>
/// <returns>Integer array containing next greater elements</returns>
public static int[] FindNextGreaterElement(int[] nums)
{
Stack<int> stack = new Stack<int>();
int[] result = new int[nums.Length];

for (int i = nums.Length - 1; i >= 0; i--)
{
while (stack.Count > 0 && stack.Peek() <= nums[i])
{
stack.Pop();
}
result[i] = stack.Count == 0 ? -1 : stack.Peek();
stack.Push(nums[i]);
}
return result;
}
}
}
40 changes: 40 additions & 0 deletions DataStructures/Stack/ReverseStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
namespace DataStructures.Stack
{
/// <summary>
/// Reverses the elements in a stack using recursion.
/// @author Mohit Singh
/// @author <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
/// </summary>
public class ReverseStack

Check warning on line 9 in DataStructures/Stack/ReverseStack.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

DataStructures/Stack/ReverseStack.cs#L9

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
/// <summary>
/// This method reverses the elements in a stack using recursion.
/// </summary>
/// <param name="stack">A Stack of Generic Type</param>
public static void Reverse<T>(Stack<T> stack)

Check failure on line 15 in DataStructures/Stack/ReverseStack.cs

View workflow job for this annotation

GitHub Actions / build

The namespace 'DataStructures.Stack' cannot be used with type arguments

Check failure on line 15 in DataStructures/Stack/ReverseStack.cs

View workflow job for this annotation

GitHub Actions / build

The namespace 'DataStructures.Stack' cannot be used with type arguments
{
if (stack.Count == 0)
{
return;
}
T temp = stack.Pop();
Reverse(stack);
InsertAtBottom(stack, temp);
}

private static void InsertAtBottom<T>(Stack<T> stack, T value)

Check failure on line 26 in DataStructures/Stack/ReverseStack.cs

View workflow job for this annotation

GitHub Actions / build

The namespace 'DataStructures.Stack' cannot be used with type arguments

Check failure on line 26 in DataStructures/Stack/ReverseStack.cs

View workflow job for this annotation

GitHub Actions / build

The namespace 'DataStructures.Stack' cannot be used with type arguments
{
if (stack.Count == 0)
{
stack.Push(value);
}
else
{
T temp = stack.Pop();
InsertAtBottom(stack, value);
stack.Push(temp);
}
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ find more than one implementation for the same objective but using different alg
* [Array-based Stack](./DataStructures/Stack/ArrayBasedStack.cs)
* [List-based Stack](./DataStructures/Stack/ListBasedStack.cs)
* [Queue-based Stack](./DataStructures/Stack/QueueBasedStack.cs)
* [Next Greater Element](./DataStructures/Stack/NextGreaterElement.cs)
* [BalancedParenthesesChecker](./DataStructures/Stack/BalancedParenthesesChecker.cs)
* [Reverse Stack](./DataStructures/Stack/ReverseStack.cs)
* [Heap](./DataStructures/Heap)
* [Min-Max Heap](./DataStructures/Heap/MinMaxHeap.cs)
* [Binary Heap](./DataStructures/Heap/BinaryHeap.cs)
Expand Down
Loading