-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwosubstrings.cpp
More file actions
40 lines (36 loc) · 832 Bytes
/
twosubstrings.cpp
File metadata and controls
40 lines (36 loc) · 832 Bytes
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
37
38
39
40
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
int n = s.size();
bool dp[2][n+2];
dp[0][0] = false;
dp[0][1] = false;
dp[0][1] = false;
dp[1][1] = false;
// AB is 1
// BA is 0
for(int i = 1; i <n; i++){
if(s[i-1] == 'A' && s[i] == 'B'){
if(dp[0][i-1]){
cout << "YES";
return 0;
}
dp[1][i+1] = true;
dp[0][i+1] = dp[0][i];
} else if(s[i-1] == 'B' && s[i] == 'A'){
if(dp[1][i-1]){
cout << "YES";
return 0;
}
dp[0][i+1] = true;
dp[1][i+1] = dp[1][i];
}else{
dp[0][i+1] = dp[0][i];
dp[1][i+1] = dp[1][i];
}
}
cout << "NO";
return 0;
}