给定一个不完整的括号序列 S。任务是找到使其成为常规括号序列所需的右括号 ‘)’ 的数量并打印完整的括号序列。您只能在给定的括号序列的末尾添加括号。如果无法完成括号序列,请打印“IMPOSSIBLE”。
让我们以下列方式定义一个正则括号序列:
- 空字符串是一个常规的括号序列。
- 如果 s 是正则括号序列,则 (s) 是正括号序列。
- 如果 s & t 是正则括号序列,则 st 是正则括号序列。
例子:
Input : str = “(()(()(”
Output : (()(()()))
Explanation : The minimum number of ) needed to make the sequence regular are 3 which are appended at the end.
Input : str = “())(()”
Output : IMPOSSIBLE
我们需要添加最少数量的右括号 ‘)’,因此我们将计算不平衡的左括号的数量,然后我们将添加该数量的右括号。如果在任何时候结束括号的数量大于开始括号,那么答案是IMPOSSIBLE 。
下面是上述方法的实现:
C++
// C++ program to find number of closing
// brackets needed and complete a regular
// bracket sequence
#include
using namespace std;
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
void completeSuquence(string s)
{
// Finding the length of sequence
int n = s.length();
int open = 0, close = 0;
for (int i = 0; i < n; i++)
{
// Counting opening brackets
if (s[i] == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open)
{
cout << "Impossible" << endl;
return;
}
}
// If possible, print 's' and
// required closing brackets.
cout << s;
for (int i = 0; i < open - close; i++)
cout << ')';
cout << endl;
}
// Driver code
int main()
{
string s = "(()(()(";
completeSuquence(s);
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java program to find number of closing
// brackets needed and complete a regular
// bracket sequence
class GFG {
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
static void completeSequence(String s)
{
// Finding the length of sequence
int n = s.length();
int open = 0, close = 0;
for (int i = 0; i < n; i++) {
// Counting opening brackets
if (s.charAt(i) == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open) {
System.out.print("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and required closing
// brackets.
System.out.print(s);
for (int i = 0; i < open - close; i++)
System.out.print(")");
}
// Driver code
public static void main(String[] args)
{
String s = "(()(()(";
completeSequence(s);
}
}
Python 3
# Python 3 program to find number of
# closing brackets needed and complete
# a regular bracket sequence
# Function to find number of closing
# brackets and complete a regular
# bracket sequence
def completeSequence(s):
# Finding the length of sequence
n = len(s)
open = 0
close = 0
for i in range(n):
# Counting opening brackets
if (s[i] == '('):
open += 1
else:
# Counting closing brackets
close += 1
# Checking if at any position the
# number of closing bracket
# is more then answer is impossible
if (close > open):
print("IMPOSSIBLE")
return
# If possible, print 's' and
# required closing brackets.
print(s, end = "")
for i in range(open - close):
print(")", end = "")
# Driver code
if __name__ == "__main__":
s = "(()(()("
completeSequence(s)
# This code is contributed by ita_c
C#
// C# program to find number of closing
// brackets needed and complete a
// regular bracket sequence
using System;
class GFG
{
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
static void completeSequence(String s)
{
// Finding the length of sequence
int n = s.Length;
int open = 0, close = 0;
for (int i = 0; i < n; i++)
{
// Counting opening brackets
if (s[i] == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open)
{
Console.Write("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and
// required closing brackets.
Console.Write(s);
for (int i = 0; i < open - close; i++)
Console.Write(")");
}
// Driver Code
static void Main()
{
String s = "(()(()(";
completeSequence(s);
}
}
// This code is contributed
// by ANKITRAI1
PHP
$open)
{
echo ("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and
// required closing brackets.
echo ($s);
for ($i = 0; $i < $open - $close; $i++)
echo (")");
}
// Driver Code
$s = "(()(()(";
completeSequence($s);
// This code is contributed
// by ajit
?>
Javascript
输出:
(()(()()))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。