-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMPILER.cpp
36 lines (35 loc) · 919 Bytes
/
COMPILER.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
// your code goes here
int t;
cin >> t;
while(t--) {
string s;
//Input string
cin >> s;
//Variable to store current status of the expression
int sum = 0;
//Variable to store the length of valid prefix
int length = 0;
for (int i = 0;i < s.length();i++) {
//Check if current character is '<' or '>', based on it update sum variable
if (s[i] == '<') {
sum++;
} else {
sum--;
}
//If sum is less than 0, than more '>' than '<' hence expression is invalid,
//else if sum is 0, then '<' and '>' are balanced.
if (sum < 0) {
break;
} else if (sum == 0) {
length = (i + 1);
}
}
//Output length of valid string
cout << length << "\n";
}
return 0;
}