G

Untitled

public
Guest Oct 15, 2024 Never 15
Clone
C++ paste1.cpp 34 lines (34 loc) | 676 Bytes
1
bool isValid(string s)
2
{
3
string s1 = s;
4
if (s.empty())
5
{
6
return true;
7
}
8
if (s.length() == 2)
9
{
10
if (s == "[]" || s == "{}" || s == "()")
11
{
12
return true;
13
}
14
else
15
{
16
return false;
17
}
18
}
19
else
20
{
21
for (int i = 0; i < s.length() - 1; i++)
22
{
23
if ((s[i] == '[' && s[i + 1] == ']') || (s[i] == '(' && s[i + 1] == ')') || (s[i] == '{' && s[i + 1] == '}'))
24
{
25
s = s.erase(i, 2);
26
}
27
}
28
}
29
if (s == s1)
30
{
31
return false;
32
}
33
return isValid(s);
34
}