给定一个由左括号、右括号和整数组成的字符串S ,任务是打印 Yes,如果在这个字符串,整数正确表示其深度。深度是指围绕该整数的嵌套括号集的数量。否则打印否。
例子:
Input: S = “((2)((3)))”
Output: Yes
Explanantion:
No of Opening & Closing Parentheses around 2 = 2
No of Opening & Closing Parentheses around 3 = 3
Input: S = “((35)(2))”
Output: No
Explanantion:
No of Opening & Closing Parentheses around 35 = 2
No of Opening & Closing Parentheses around 2 = 2
方法:
- 迭代由字符字符串的字符
- 如果它是左括号或右括号,则附加到数组arr[]
- 如果字符是数字,则迭代直到读取整个数字,然后附加到arr[]
- 逐个元素迭代数组arr[]
- 如果元素是 ‘(‘ 增加深度并打开 1
- 如果元素是 ‘)’ 将深度减少 1 并增加接近 1
- 如果元素是一个整数,检查是否“整数!=深度”,如果为真
将标志设置为 0 并中断循环
- 遍历整个字符串,检查是否打开不等于关闭,如果真设置标志 = 0
下面是上述方法的实现
C++
// Function to check if the Depth
// of Parentheses is correct
// in the given String
#include
using namespace std;
bool Formatted(string s)
{
vector k;
int i = 0;
while (i < s.size())
{
// Appending if the
// Character is not integer
if (s[i]==')' or s[i]=='(')
{
k.push_back(s[i]);
i += 1;
}
else
{
// Iterating till the entire
// Digit is read
char st;
while (s[i]!=')' and s[i]!=')')
{
st = s[i];
i = i + 1;
}
k.push_back(st);
}
}
int depth = 0, flag = 1;
int open = 0, close = 0;
for (char i:k)
{
// Check if character is '('
if (i == '(')
{
// Increment depth by 1
depth += 1;
// Increment open by 1
open += 1;
// Check if character is ')'
}else if (i == ')'){
// Decrement depth by 1
depth -= 1;
// Increment close by 1
close += 1;
}
else{
if (i-'0' != depth){
flag = 0;
break;
}
}
}
// Check if open parentheses
// NOT equals close parentheses
if (open != close)
flag = 0;
return (flag == 1)?true:false;
}
// Driver Code
int main()
{
string s = "((2)((3)))";
bool k = Formatted(s);
if (k == true)
printf("Yes");
else
printf("No");
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Function to check if the Depth
// of Parentheses is correct
// in the given String
import java.util.*;
class GFG{
static boolean Formatted(String s)
{
Vector k = new Vector();
int i = 0;
while (i < s.length())
{
// Appending if the
// Character is not integer
if (s.charAt(i)==')' || s.charAt(i)=='(')
{
k.add(s.charAt(i));
i += 1;
}
else
{
// Iterating till the entire
// Digit is read
char st = 0;
while (s.charAt(i)!=')' && s.charAt(i)!=')')
{
st = s.charAt(i);
i = i + 1;
}
k.add(st);
}
}
int depth = 0, flag = 1;
int open = 0, close = 0;
for (char i2 : k)
{
// Check if character is '('
if (i2 == '(')
{
// Increment depth by 1
depth += 1;
// Increment open by 1
open += 1;
// Check if character is ')'
}else if (i2 == ')'){
// Decrement depth by 1
depth -= 1;
// Increment close by 1
close += 1;
}
else{
if (i2-'0' != depth){
flag = 0;
break;
}
}
}
// Check if open parentheses
// NOT equals close parentheses
if (open != close)
flag = 0;
return (flag == 1)?true:false;
}
// Driver Code
public static void main(String[] args)
{
String s = "((2)((3)))";
boolean k = Formatted(s);
if (k == true)
System.out.printf("Yes");
else
System.out.printf("No");
}
}
// This code is contributed by Rajput-Ji
Python3
# Function to check if the Depth
# of Parentheses is correct
# in the given String
def Formatted(s):
k = []
i = 0
while i < len(s):
# Appending if the
# Character is not integer
if s[i].isdigit() == False:
k.append(s[i])
i += 1
else:
# Iterating till the entire
# Digit is read
st = ""
while s[i].isdigit():
st += s[i]
i = i + 1
k.append(int(st))
depth, flag = 0, 1
open, close = 0, 0
for i in k:
# Check if character is '('
if i == '(':
# Increment depth by 1
depth += 1
# Increment open by 1
open += 1
# Check if character is ')'
elif i == ')':
# Decrement depth by 1
depth -= 1
# Increment close by 1
close += 1
else:
if i != depth:
flag = 0
break
# Check if open parentheses
# NOT equals close parentheses
if open != close:
flag = 0
return True if flag == 1 else False
# Driver Code
if __name__ == '__main__':
s = '((2)((3)))'
k = Formatted(s)
if k == True:
print("Yes")
else:
print("No")
C#
// Function to check if the Depth
// of Parentheses is correct
// in the given String
using System;
using System.Collections.Generic;
class GFG
{
static bool Formatted(String s)
{
List k = new List();
int i = 0;
while (i < s.Length)
{
// Appending if the
// char is not integer
if (s[i]==')' || s[i]=='(')
{
k.Add(s[i]);
i += 1;
}
else
{
// Iterating till the entire
// Digit is read
char st = '\x0000';
while (s[i] != ')' && s[i] != ')')
{
st = s[i];
i = i + 1;
}
k.Add(st);
}
}
int depth = 0, flag = 1;
int open = 0, close = 0;
foreach (char i2 in k)
{
// Check if character is '('
if (i2 == '(')
{
// Increment depth by 1
depth += 1;
// Increment open by 1
open += 1;
// Check if character is ')'
}else if (i2 == ')'){
// Decrement depth by 1
depth -= 1;
// Increment close by 1
close += 1;
}
else{
if (i2-'0' != depth){
flag = 0;
break;
}
}
}
// Check if open parentheses
// NOT equals close parentheses
if (open != close)
flag = 0;
return (flag == 1)?true:false;
}
// Driver Code
public static void Main(String[] args)
{
String s = "((2)((3)))";
bool k = Formatted(s);
if (k == true)
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by 29AjayKumar
输出:
Yes
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live