Count cost to balance the parentheses

Here given code implementation process.

/*
    Java program for
    Count cost to balance the parentheses
*/
public class Cost
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public int getCost(String text)
	{
		int n = text.length();
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		int track = 0;
		int result = 0;
		int[] count = new int[n];
		for (int i = 0; i < n; ++i)
		{
			if (text.charAt(i) == '(')
			{
				// When open bracket
				track++;
			}
			else
			{
				// When close bracket
				track--;
			}
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text.charAt(0) == '(')
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		for (int i = 1; i < n; ++i)
		{
			if (text.charAt(i) == '(')
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += absValue(count[i]);
			}
		}
		return result;
	}
	public static void main(String[] args)
	{
		Cost task = new Cost();
		String text1 = "(())))(()(";
		String text2 = "())(";
		String text3 = "()))((()";
		/*
		    Example 1
		    ----------
		    text = (())))(()(
		    ────────────────────────

		        (()) ))(()(
		            │  │
		            └──┘         ①
		            cost 2

		    Move character at 6th to 4th position

		        (()) ))(()(  
		              ││   first move
		              └┘  
		        (()) )()()(   After move
		             ││   second move
		             └┘      
		        (()) ())()( <- After move
		    ──────────────────────── 
		        
		        (())() )()(
		              │   │      ②
		              └───┘ 
		              cost 3

		        (())() )()(
		                 ││   first move
		                 └┘    
		        (())() )(()
		                ││   second move
		                └┘    
		        (())() )(()
		               ││    Third move
		               └┘  
		        (())() ()()

		    Move character at 9th to 6th position
		    ──────────────────────── 

		    Total cost : (2 + 3) = 5
		*/
		System.out.println(task.getCost(text1));
		/*
		    Example 2
		    ----------
		    text = ())(
		    ─────────────            
		        () )(
		          │ │      ①
		          └─┘ 
		          Cost 1
		    Move character at 3th to 2th position

		        ()() <- balance parentheses
		    ──────────────────────── 
		    Total cost : 1
		*/
		System.out.println(task.getCost(text2));
		/*
            Example 3
            ----------
            text = ()))((()
            ─────────────            
                () ))((()
                  │  │      ①
                  └──┘ 
                  Cost 2
            Move character at 4th to 2th position
                 ()())(()<- after parentheses
            ──────────────────────── 
      
                ()() )(()
                    │ │      ②
                    └─┘ 
                  Cost 2
            Move character at 4th to 2th position
                 ()()()()<- after parentheses
            ──────────────────────── 
    
            Total cost : (2 + 2) = 4
        */
		System.out.println(task.getCost(text3));
	}
}

Output

5
1
4
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Count cost to balance the parentheses
*/
class Cost
{
	public: int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	int getCost(string text)
	{
		int n = text.length();
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		int track = 0;
		int result = 0;
		int count[n];
		for (int i = 0; i < n; ++i)
		{
			if (text[i] == '(')
			{
				// When open bracket
				track++;
			}
			else
			{
				// When close bracket
				track--;
			}
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text[0] == '(')
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		for (int i = 1; i < n; ++i)
		{
			if (text[i] == '(')
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += this->absValue(count[i]);
			}
		}
		return result;
	}
};
int main()
{
	Cost *task = new Cost();
	string text1 = "(())))(()(";
	string text2 = "())(";
	string text3 = "()))((()";
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	cout << task->getCost(text1) << endl;
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	cout << task->getCost(text2) << endl;
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	cout << task->getCost(text3) << endl;
	return 0;
}

Output

5
1
4
// Include namespace system
using System;
/*
    Csharp program for
    Count cost to balance the parentheses
*/
public class Cost
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public int getCost(String text)
	{
		int n = text.Length;
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		int track = 0;
		int result = 0;
		int[] count = new int[n];
		for (int i = 0; i < n; ++i)
		{
			if (text[i] == '(')
			{
				// When open bracket
				track++;
			}
			else
			{
				// When close bracket
				track--;
			}
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text[0] == '(')
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		for (int i = 1; i < n; ++i)
		{
			if (text[i] == '(')
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += this.absValue(count[i]);
			}
		}
		return result;
	}
	public static void Main(String[] args)
	{
		Cost task = new Cost();
		String text1 = "(())))(()(";
		String text2 = "())(";
		String text3 = "()))((()";
		/*
		    Example 1
		    ----------
		    text = (())))(()(
		    ────────────────────────
		        (()) ))(()(
		            │  │
		            └──┘         ①
		            cost 2
		    Move character at 6th to 4th position
		        (()) ))(()(  
		              ││   first move
		              └┘  
		        (()) )()()(   After move
		             ││   second move
		             └┘      
		        (()) ())()( <- After move
		    ──────────────────────── 
		        
		        (())() )()(
		              │   │      ②
		              └───┘ 
		              cost 3
		        (())() )()(
		                 ││   first move
		                 └┘    
		        (())() )(()
		                ││   second move
		                └┘    
		        (())() )(()
		               ││    Third move
		               └┘  
		        (())() ()()
		    Move character at 9th to 6th position
		    ──────────────────────── 
		    Total cost : (2 + 3) = 5
		*/
		Console.WriteLine(task.getCost(text1));
		/*
		    Example 2
		    ----------
		    text = ())(
		    ─────────────            
		        () )(
		          │ │      ①
		          └─┘ 
		          Cost 1
		    Move character at 3th to 2th position
		        ()() <- balance parentheses
		    ──────────────────────── 
		    Total cost : 1
		*/
		Console.WriteLine(task.getCost(text2));
		/*
		    Example 3
		    ----------
		    text = ()))((()
		    ─────────────            
		        () ))((()
		          │  │      ①
		          └──┘ 
		          Cost 2
		    Move character at 4th to 2th position
		         ()())(()<- after parentheses
		    ──────────────────────── 

		        ()() )(()
		            │ │      ②
		            └─┘ 
		          Cost 2
		    Move character at 4th to 2th position
		         ()()()()<- after parentheses
		    ──────────────────────── 

		    Total cost : (2 + 2) = 4
		        
		*/
		Console.WriteLine(task.getCost(text3));
	}
}

Output

5
1
4
package main
import "fmt"
/*
    Go program for
    Count cost to balance the parentheses
*/

func absValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func getCost(text string) int {
	var n int = len(text)
	if n == 0 {
		// Empty text
		return -1
	}
	var track int = 0
	var result int = 0
	var count = make([] int, n)
	for i := 0 ; i < n ; i++ {
		if text[i] == '(' {
			// When open bracket
			track++
		} else {
			// When close bracket
			track--
		}
	}
	if track != 0 {
		// Means open and close bracket are not same
		return -1
	}
	if text[0] == '(' {
		count[0] = 1
	} else {
		count[0] = -1
		result = 1
	}
	for i := 1 ; i < n ; i++ {
		if text[i] == '(' {
			count[i] = count[i - 1] + 1
		} else {
			count[i] = count[i - 1] - 1
		}
		if count[i] < 0 {
			result += absValue(count[i])
		}
	}
	return result
}
func main() {

	var text1 string = "(())))(()("
	var text2 string = "())("
	var text3 string = "()))((()"
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	fmt.Println(getCost(text1))
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	fmt.Println(getCost(text2))
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	fmt.Println(getCost(text3))
}

Output

5
1
4
<?php
/*
    Php program for
    Count cost to balance the parentheses
*/
class Cost
{
	public	function absValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function getCost($text)
	{
		$n = strlen($text);
		if ($n == 0)
		{
			// Empty text
			return -1;
		}
		$track = 0;
		$result = 0;
		$count = array_fill(0, $n, 0);
		for ($i = 0; $i < $n; ++$i)
		{
			if ($text[$i] == '(')
			{
				// When open bracket
				$track++;
			}
			else
			{
				// When close bracket
				$track--;
			}
		}
		if ($track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if ($text[0] == '(')
		{
			$count[0] = 1;
		}
		else
		{
			$count[0] = -1;
			$result = 1;
		}
		for ($i = 1; $i < $n; ++$i)
		{
			if ($text[$i] == '(')
			{
				$count[$i] = $count[$i - 1] + 1;
			}
			else
			{
				$count[$i] = $count[$i - 1] - 1;
			}
			if ($count[$i] < 0)
			{
				$result += $this->absValue($count[$i]);
			}
		}
		return $result;
	}
}

function main()
{
	$task = new Cost();
	$text1 = "(())))(()(";
	$text2 = "())(";
	$text3 = "()))((()";
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	echo($task->getCost($text1).
		"\n");
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	echo($task->getCost($text2).
		"\n");
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	echo($task->getCost($text3).
		"\n");
}
main();

Output

5
1
4
/*
    Node JS program for
    Count cost to balance the parentheses
*/
class Cost
{
	absValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	getCost(text)
	{
		var n = text.length;
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		var track = 0;
		var result = 0;
		var count = Array(n).fill(0);
		for (var i = 0; i < n; ++i)
		{
			if (text.charAt(i) == '(')
			{
				// When open bracket
				track++;
			}
			else
			{
				// When close bracket
				track--;
			}
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text.charAt(0) == '(')
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		for (var i = 1; i < n; ++i)
		{
			if (text.charAt(i) == '(')
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += this.absValue(count[i]);
			}
		}
		return result;
	}
}

function main()
{
	var task = new Cost();
	var text1 = "(())))(()(";
	var text2 = "())(";
	var text3 = "()))((()";
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	console.log(task.getCost(text1));
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	console.log(task.getCost(text2));
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	console.log(task.getCost(text3));
}
main();

Output

5
1
4
#    Python 3 program for
#    Count cost to balance the parentheses
class Cost :
	def absValue(self, x) :
		if (x < 0) :
			return -x
		
		return x
	
	def getCost(self, text) :
		n = len(text)
		if (n == 0) :
			#  Empty text
			return -1
		
		track = 0
		result = 0
		count = [0] * (n)
		i = 0
		while (i < n) :
			if (text[i] == '(') :
				#  When open bracket
				track += 1
			else :
				#  When close bracket
				track -= 1
			
			i += 1
		
		if (track != 0) :
			#  Means open and close bracket are not same
			return -1
		
		if (text[0] == '(') :
			count[0] = 1
		else :
			count[0] = -1
			result = 1
		
		i = 1
		while (i < n) :
			if (text[i] == '(') :
				count[i] = count[i - 1] + 1
			else :
				count[i] = count[i - 1] - 1
			
			if (count[i] < 0) :
				result += self.absValue(count[i])
			
			i += 1
		
		return result
	

def main() :
	task = Cost()
	text1 = "(())))(()("
	text2 = "())("
	text3 = "()))((()"
	#    Example 1
	#    ----------
	#    text = (())))(()(
	#    ────────────────────────
	#        (()) ))(()(
	#            │  │
	#            └──┘         ①
	#            cost 2
	#    Move character at 6th to 4th position
	#        (()) ))(()(  
	#              ││   first move
	#              └┘  
	#        (()) )()()(   After move
	#             ││   second move
	#             └┘      
	#        (()) ())()( <- After move
	#    ──────────────────────── 
	#        (())() )()(
	#              │   │      ②
	#              └───┘ 
	#              cost 3
	#        (())() )()(
	#                 ││   first move
	#                 └┘    
	#        (())() )(()
	#                ││   second move
	#                └┘    
	#        (())() )(()
	#               ││    Third move
	#               └┘  
	#        (())() ()()
	#    Move character at 9th to 6th position
	#    ──────────────────────── 
	#    Total cost : (2 + 3) = 5
	print(task.getCost(text1))
	#    Example 2
	#    ----------
	#    text = ())(
	#    ─────────────            
	#        () )(
	#          │ │      ①
	#          └─┘ 
	#          Cost 1
	#    Move character at 3th to 2th position
	#        ()() <- balance parentheses
	#    ──────────────────────── 
	#    Total cost : 1
	print(task.getCost(text2))
	#    Example 3
	#    ----------
	#    text = ()))((()
	#    ─────────────            
	#        () ))((()
	#          │  │      ①
	#          └──┘ 
	#          Cost 2
	#    Move character at 4th to 2th position
	#         ()())(()<- after parentheses
	#    ──────────────────────── 
	#        ()() )(()
	#            │ │      ②
	#            └─┘ 
	#          Cost 2
	#    Move character at 4th to 2th position
	#         ()()()()<- after parentheses
	#    ──────────────────────── 
	#    Total cost : (2 + 2) = 4
	print(task.getCost(text3))

if __name__ == "__main__": main()

Output

5
1
4
#    Ruby program for
#    Count cost to balance the parentheses
class Cost 
	def absValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def getCost(text) 
		n = text.length
		if (n == 0) 
			#  Empty text
			return -1
		end

		track = 0
		result = 0
		count = Array.new(n) {0}
		i = 0
		while (i < n) 
			if (text[i] == '(') 
				#  When open bracket
				track += 1
			else
 
				#  When close bracket
				track -= 1
			end

			i += 1
		end

		if (track != 0) 
			#  Means open and close bracket are not same
			return -1
		end

		if (text[0] == '(') 
			count[0] = 1
		else
 
			count[0] = -1
			result = 1
		end

		i = 1
		while (i < n) 
			if (text[i] == '(') 
				count[i] = count[i - 1] + 1
			else
 
				count[i] = count[i - 1] - 1
			end

			if (count[i] < 0) 
				result += self.absValue(count[i])
			end

			i += 1
		end

		return result
	end

end

def main() 
	task = Cost.new()
	text1 = "(())))(()("
	text2 = "())("
	text3 = "()))((()"
	#    Example 1
	#    ----------
	#    text = (())))(()(
	#    ────────────────────────
	#        (()) ))(()(
	#            │  │
	#            └──┘         ①
	#            cost 2
	#    Move character at 6th to 4th position
	#        (()) ))(()(  
	#              ││   first move
	#              └┘  
	#        (()) )()()(   After move
	#             ││   second move
	#             └┘      
	#        (()) ())()( <- After move
	#    ──────────────────────── 
	#        (())() )()(
	#              │   │      ②
	#              └───┘ 
	#              cost 3
	#        (())() )()(
	#                 ││   first move
	#                 └┘    
	#        (())() )(()
	#                ││   second move
	#                └┘    
	#        (())() )(()
	#               ││    Third move
	#               └┘  
	#        (())() ()()
	#    Move character at 9th to 6th position
	#    ──────────────────────── 
	#    Total cost : (2 + 3) = 5
	print(task.getCost(text1), "\n")
	#    Example 2
	#    ----------
	#    text = ())(
	#    ─────────────            
	#        () )(
	#          │ │      ①
	#          └─┘ 
	#          Cost 1
	#    Move character at 3th to 2th position
	#        ()() <- balance parentheses
	#    ──────────────────────── 
	#    Total cost : 1
	print(task.getCost(text2), "\n")
	#    Example 3
	#    ----------
	#    text = ()))((()
	#    ─────────────            
	#        () ))((()
	#          │  │      ①
	#          └──┘ 
	#          Cost 2
	#    Move character at 4th to 2th position
	#         ()())(()<- after parentheses
	#    ──────────────────────── 
	#        ()() )(()
	#            │ │      ②
	#            └─┘ 
	#          Cost 2
	#    Move character at 4th to 2th position
	#         ()()()()<- after parentheses
	#    ──────────────────────── 
	#    Total cost : (2 + 2) = 4
	print(task.getCost(text3), "\n")
end

main()

Output

5
1
4
import scala.collection.mutable._;
/*
    Scala program for
    Count cost to balance the parentheses
*/
class Cost()
{
	def absValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def getCost(text: String): Int = {
		var n: Int = text.length();
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		var track: Int = 0;
		var result: Int = 0;
		var count: Array[Int] = Array.fill[Int](n)(0);
		var i: Int = 0;
		while (i < n)
		{
			if (text.charAt(i) == '(')
			{
				// When open bracket
				track += 1;
			}
			else
			{
				// When close bracket
				track -= 1;
			}
			i += 1;
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text.charAt(0) == '(')
		{
			count(0) = 1;
		}
		else
		{
			count(0) = -1;
			result = 1;
		}
		i = 1;
		while (i < n)
		{
			if (text.charAt(i) == '(')
			{
				count(i) = count(i - 1) + 1;
			}
			else
			{
				count(i) = count(i - 1) - 1;
			}
			if (count(i) < 0)
			{
				result += absValue(count(i));
			}
			i += 1;
		}
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Cost = new Cost();
		var text1: String = "(())))(()(";
		var text2: String = "())(";
		var text3: String = "()))((()";
		/*
		    Example 1
		    ----------
		    text = (())))(()(
		    ────────────────────────
		        (()) ))(()(
		            │  │
		            └──┘         ①
		            cost 2
		    Move character at 6th to 4th position
		        (()) ))(()(  
		              ││   first move
		              └┘  
		        (()) )()()(   After move
		             ││   second move
		             └┘      
		        (()) ())()( <- After move
		    ──────────────────────── 
		        
		        (())() )()(
		              │   │      ②
		              └───┘ 
		              cost 3
		        (())() )()(
		                 ││   first move
		                 └┘    
		        (())() )(()
		                ││   second move
		                └┘    
		        (())() )(()
		               ││    Third move
		               └┘  
		        (())() ()()
		    Move character at 9th to 6th position
		    ──────────────────────── 
		    Total cost : (2 + 3) = 5
		*/
		println(task.getCost(text1));
		/*
		    Example 2
		    ----------
		    text = ())(
		    ─────────────            
		        () )(
		          │ │      ①
		          └─┘ 
		          Cost 1
		    Move character at 3th to 2th position
		        ()() <- balance parentheses
		    ──────────────────────── 
		    Total cost : 1
		*/
		println(task.getCost(text2));
		/*
		    Example 3
		    ----------
		    text = ()))((()
		    ─────────────            
		        () ))((()
		          │  │      ①
		          └──┘ 
		          Cost 2
		    Move character at 4th to 2th position
		         ()())(()<- after parentheses
		    ──────────────────────── 

		        ()() )(()
		            │ │      ②
		            └─┘ 
		          Cost 2
		    Move character at 4th to 2th position
		         ()()()()<- after parentheses
		    ──────────────────────── 

		    Total cost : (2 + 2) = 4
		        
		*/
		println(task.getCost(text3));
	}
}

Output

5
1
4
import Foundation;
/*
    Swift 4 program for
    Count cost to balance the parentheses
*/
class Cost
{
	func absValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func getCost(_ data: String) -> Int
	{	
      	let text = Array(data);
		let n: Int = text.count;
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		var track: Int = 0;
		var result: Int = 0;
		var count: [Int] = Array(repeating: 0, count: n);
		var i: Int = 0;
		while (i < n)
		{
			if (text[i] == "(")
			{
				// When open bracket
				track += 1;
			}
			else
			{
				// When close bracket
				track -= 1;
			}
			i += 1;
		}
		if (track  != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text[0] == "(")
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		i = 1;
		while (i < n)
		{
			if (text[i] == "(")
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += self.absValue(count[i]);
			}
			i += 1;
		}
		return result;
	}
}
func main()
{
	let task: Cost = Cost();
	let text1: String = "(())))(()(";
	let text2: String = "())(";
	let text3: String = "()))((()";
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	print(task.getCost(text1));
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	print(task.getCost(text2));
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	print(task.getCost(text3));
}
main();

Output

5
1
4
/*
    Kotlin program for
    Count cost to balance the parentheses
*/
class Cost
{
	fun absValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun getCost(text: String): Int
	{
		val n: Int = text.length;
		if (n == 0)
		{
			// Empty text
			return -1;
		}
		var track: Int = 0;
		var result: Int = 0;
		val count: Array < Int > = Array(n)
		{
			0
		};
		var i: Int = 0;
		while (i < n)
		{
			if (text.get(i) == '(')
			{
				// When open bracket
				track += 1;
			}
			else
			{
				// When close bracket
				track -= 1;
			}
			i += 1;
		}
		if (track != 0)
		{
			// Means open and close bracket are not same
			return -1;
		}
		if (text.get(0) == '(')
		{
			count[0] = 1;
		}
		else
		{
			count[0] = -1;
			result = 1;
		}
		i = 1;
		while (i < n)
		{
			if (text.get(i) == '(')
			{
				count[i] = count[i - 1] + 1;
			}
			else
			{
				count[i] = count[i - 1] - 1;
			}
			if (count[i] < 0)
			{
				result += this.absValue(count[i]);
			}
			i += 1;
		}
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Cost = Cost();
	val text1: String = "(())))(()(";
	val text2: String = "())(";
	val text3: String = "()))((()";
	/*
	    Example 1
	    ----------
	    text = (())))(()(
	    ────────────────────────
	        (()) ))(()(
	            │  │
	            └──┘         ①
	            cost 2
	    Move character at 6th to 4th position
	        (()) ))(()(  
	              ││   first move
	              └┘  
	        (()) )()()(   After move
	             ││   second move
	             └┘      
	        (()) ())()( <- After move
	    ──────────────────────── 
	        
	        (())() )()(
	              │   │      ②
	              └───┘ 
	              cost 3
	        (())() )()(
	                 ││   first move
	                 └┘    
	        (())() )(()
	                ││   second move
	                └┘    
	        (())() )(()
	               ││    Third move
	               └┘  
	        (())() ()()
	    Move character at 9th to 6th position
	    ──────────────────────── 
	    Total cost : (2 + 3) = 5
	*/
	println(task.getCost(text1));
	/*
	    Example 2
	    ----------
	    text = ())(
	    ─────────────            
	        () )(
	          │ │      ①
	          └─┘ 
	          Cost 1
	    Move character at 3th to 2th position
	        ()() <- balance parentheses
	    ──────────────────────── 
	    Total cost : 1
	*/
	println(task.getCost(text2));
	/*
	    Example 3
	    ----------
	    text = ()))((()
	    ─────────────            
	        () ))((()
	          │  │      ①
	          └──┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()())(()<- after parentheses
	    ──────────────────────── 

	        ()() )(()
	            │ │      ②
	            └─┘ 
	          Cost 2
	    Move character at 4th to 2th position
	         ()()()()<- after parentheses
	    ──────────────────────── 

	    Total cost : (2 + 2) = 4
	        
	*/
	println(task.getCost(text3));
}

Output

5
1
4


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