Minimum add to make parentheses valid

Here given code implementation process.

// C Program
// Minimum add to make parentheses valid
#include <stdio.h>

// Find number of parentheses to make a valid parentheses
void validParentheses(char parenthes[], int size)
{
	// Define calculated variables
	int bracket = 0;
	int result = 0;
	for (int i = 0; i < size; ++i)
	{
		if (parenthes[i] == '(')
		{
			bracket += 1;
		}
		else
		{
			bracket -= 1;
		}
		if (bracket == -1)
		{
			result += 1;
			bracket += 1;
		}
	}
	// Display calculated result
	printf(" [%s] : %d \n", parenthes, result + bracket);
}
int main(int argc, char const *argv[])
{
	// Given input parentheses
	char p1[] = "(())))(((";
	char p2[] = "()()()";
	char p3[] = ")(()(()";
  
	// Test
	int size = sizeof(p1) / sizeof(p1[0]) - 1;
	validParentheses(p1, size);
	size = sizeof(p2) / sizeof(p2[0]) - 1;
	validParentheses(p2, size);
	size = sizeof(p3) / sizeof(p3[0]) - 1;
	validParentheses(p3, size);
	return 0;
}

Output

 [(())))(((] : 5
 [()()()] : 0
 [)(()(()] : 3
/*
  Java program
  Minimum add to make parentheses valid
*/
public class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	public void validParentheses(String parenthes)
	{
		int size = parenthes.length();
		// Define calculated variables
		int bracket = 0;
		int result = 0;
		for (int i = 0; i < size; ++i)
		{
			if (parenthes.charAt(i) == '(')
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result += 1;
				bracket += 1;
			}
		}
		// Display calculated result
		System.out.print(" " + parenthes + " : " + (result + bracket) + " \n");
	}
	public static void main(String[] args)
	{
		ParenthesesCheck task = new ParenthesesCheck();
		// Given input parentheses
		String p1 = "(())))(((";
		String p2 = "()()()";
		String p3 = ")(()(()";
		// Test
		task.validParentheses(p1);
		task.validParentheses(p2);
		task.validParentheses(p3);
	}
}

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
// Include header file
#include <iostream>
#include<string.h>
using namespace std;
/*
  C++ program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	public:
		// Find number of parentheses to make a valid parentheses
		void validParentheses(string parenthes)
		{
			int size = parenthes.size();
			// Define calculated variables
			int bracket = 0;
			int result = 0;
			for (int i = 0; i < size; ++i)
			{
				if (parenthes[i] == '(')
				{
					bracket += 1;
				}
				else
				{
					bracket -= 1;
				}
				if (bracket == -1)
				{
					result += 1;
					bracket += 1;
				}
			}
			// Display calculated result
			cout << " " << parenthes << " : " << (result + bracket) << " \n";
		}
};
int main()
{
	ParenthesesCheck task = ParenthesesCheck();
	// Given input parentheses
	string p1 = "(())))(((";
	string p2 = "()()()";
	string p3 = ")(()(()";
	// Test
	task.validParentheses(p1);
	task.validParentheses(p2);
	task.validParentheses(p3);
	return 0;
}

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
// Include namespace system
using System;
/*
  C# program
  Minimum add to make parentheses valid
*/
public class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	public void validParentheses(String parenthes)
	{
		int size = parenthes.Length;
		// Define calculated variables
		int bracket = 0;
		int result = 0;
		for (int i = 0; i < size; ++i)
		{
			if (parenthes[i] == '(')
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result += 1;
				bracket += 1;
			}
		}
		// Display calculated result
		Console.Write(" " + parenthes + " : " + (result + bracket) + " \n");
	}
	public static void Main(String[] args)
	{
		ParenthesesCheck task = new ParenthesesCheck();
		// Given input parentheses
		String p1 = "(())))(((";
		String p2 = "()()()";
		String p3 = ")(()(()";
		// Test
		task.validParentheses(p1);
		task.validParentheses(p2);
		task.validParentheses(p3);
	}
}

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
<?php
/*
  Php program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	public	function validParentheses($parenthes)
	{
		$size = strlen($parenthes);
		// Define calculated variables
		$bracket = 0;
		$result = 0;
		for ($i = 0; $i < $size; ++$i)
		{
			if ($parenthes[$i] == '(')
			{
				$bracket += 1;
			}
			else
			{
				$bracket -= 1;
			}
			if ($bracket == -1)
			{
				$result += 1;
				$bracket += 1;
			}
		}
		// Display calculated result
		echo " ". $parenthes ." : ". ($result + $bracket) ." \n";
	}
}

function main()
{
	$task = new ParenthesesCheck();
	// Given input parentheses
	$p1 = "(())))(((";
	$p2 = "()()()";
	$p3 = ")(()(()";
	// Test
	$task->validParentheses($p1);
	$task->validParentheses($p2);
	$task->validParentheses($p3);
}
main();

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
/*
  Node Js program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	validParentheses(parenthes)
	{
		var size = parenthes.length;
		// Define calculated variables
		var bracket = 0;
		var result = 0;
		for (var i = 0; i < size; ++i)
		{
			if (parenthes[i] == '(')
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result += 1;
				bracket += 1;
			}
		}
		// Display calculated result
		process.stdout.write(" " + parenthes + " : " + (result + bracket) + " \n");
	}
}

function main()
{
	var task = new ParenthesesCheck();
	// Given input parentheses
	var p1 = "(())))(((";
	var p2 = "()()()";
	var p3 = ")(()(()";
	// Test
	task.validParentheses(p1);
	task.validParentheses(p2);
	task.validParentheses(p3);
}
main();

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
#   Python 3 program
#   Minimum add to make parentheses valid

class ParenthesesCheck :
	#  Find number of parentheses to make a valid parentheses
	def validParentheses(self, parenthes) :
		size = len(parenthes)
		#  Define calculated variables
		bracket = 0
		result = 0
		i = 0
		while (i < size) :
			if (parenthes[i] == '(') :
				bracket += 1
			else :
				bracket -= 1
			
			if (bracket == -1) :
				result += 1
				bracket += 1
			
			i += 1
		
		#  Display calculated result
		print(" ", parenthes ," : ", (result + bracket) ," ")
	

def main() :
	task = ParenthesesCheck()
	#  Given input parentheses
	p1 = "(())))((("
	p2 = "()()()"
	p3 = ")(()(()"
	#  Test
	task.validParentheses(p1)
	task.validParentheses(p2)
	task.validParentheses(p3)

if __name__ == "__main__": main()

Output

  (())))(((  :  5
  ()()()  :  0
  )(()(()  :  3
#   Ruby program
#   Minimum add to make parentheses valid

class ParenthesesCheck 
	#  Find number of parentheses to make a valid parentheses
	def validParentheses(parenthes) 
		size = parenthes.length()
		#  Define calculated variables
		bracket = 0
		result = 0
		i = 0
		while (i < size) 
			if (parenthes[i] == '(') 
				bracket += 1
			else 
				bracket -= 1
			end

			if (bracket == -1) 
				result += 1
				bracket += 1
			end

			i += 1
		end

		#  Display calculated result
		print(" ", parenthes ," : ", (result + bracket) ," \n")
	end

end

def main() 
	task = ParenthesesCheck.new()
	#  Given input parentheses
	p1 = "(())))((("
	p2 = "()()()"
	p3 = ")(()(()"
	#  Test
	task.validParentheses(p1)
	task.validParentheses(p2)
	task.validParentheses(p3)
end

main()

Output

 (())))((( : 5 
 ()()() : 0 
 )(()(() : 3 
/*
  Scala program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	def validParentheses(parenthes: String): Unit = {
		var size: Int = parenthes.length();
		// Define calculated variables
		var bracket: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if (parenthes(i) == '(')
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result  += 1;
				bracket += 1;
			}
			i += 1;
		}
		// Display calculated result
		print(" " + parenthes + " : " + (result + bracket) + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ParenthesesCheck = new ParenthesesCheck();
		// Given input parentheses
		var p1: String = "(())))(((";
		var p2: String = "()()()";
		var p3: String = ")(()(()";
		// Test
		task.validParentheses(p1);
		task.validParentheses(p2);
		task.validParentheses(p3);
	}
}

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3
/*
  Swift 4 program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	func validParentheses(_ text: String)
	{
        let parenthes = Array(text);
		let size: Int = parenthes.count;
		// Define calculated variables
		var bracket: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if (parenthes[i] == "(")
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result += 1;
				bracket += 1;
			}
			i += 1;
		}
		// Display calculated result
		print(" ", text ," : ", (result + bracket) ," ");
	}
}
func main()
{
	let task: ParenthesesCheck = ParenthesesCheck();
	// Given input parentheses
	let p1: String = "(())))(((";
	let p2: String = "()()()";
	let p3: String = ")(()(()";
	// Test
	task.validParentheses(p1);
	task.validParentheses(p2);
	task.validParentheses(p3);
}
main();

Output

  (())))(((  :  5
  ()()()  :  0
  )(()(()  :  3
/*
  Kotlin program
  Minimum add to make parentheses valid
*/
class ParenthesesCheck
{
	// Find number of parentheses to make a valid parentheses
	fun validParentheses(parenthes: String): Unit
	{
		var size: Int = parenthes.length;
		// Define calculated variables
		var bracket: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if (parenthes[i] == '(')
			{
				bracket += 1;
			}
			else
			{
				bracket -= 1;
			}
			if (bracket == -1)
			{
				result += 1;
				bracket += 1;
			}
			i += 1;
		}
		// Display calculated result
		print(" " + parenthes + " : " + (result + bracket) + " \n");
	}
}
fun main(args: Array<String>): Unit
{
	var task: ParenthesesCheck = ParenthesesCheck();
	// Given input parentheses
	var p1: String = "(())))(((";
	var p2: String = "()()()";
	var p3: String = ")(()(()";
	// Test
	task.validParentheses(p1);
	task.validParentheses(p2);
	task.validParentheses(p3);
}

Output

 (())))((( : 5
 ()()() : 0
 )(()(() : 3


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved