MaxDepth.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static int MaxDepth(string s) {
		int depth = 0;
		int curDepth = 0;
		
		for (int i = 0; i < s.Length; i++)
		{
			curDepth = s[i] == '(' ? curDepth + 1 : s[i] == ')' ? curDepth - 1 : curDepth;
			depth = Math.Max(depth, curDepth);
		}

		return depth;
	}
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		Console.WriteLine(MaxDepth("(4)+((5))+(((6)))")); // 3
		Console.WriteLine(MaxDepth("(1+(2*3)+((8)/4))+1")); // 3
	}
}
;