Codeforces Round #306 (Div. 2) 550A - Two Substrings Solution
Problem Link: https://codeforces.com/problemset/problem/550/A
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- string s;
- cin >> s;
- int i;
- int l = s.size();
- bool a = false, b = false, c = false, d = false;
- for (int i = 0; i < l - 1; i++) {
- if ((s[i] == 'B') && (s[i + 1] == 'A') && (s[i + 2] == 'B') &&
- (d == false) && (c == false))
- d = true, i += 2;
- else if ((s[i] == 'A') && (s[i + 1] == 'B') && (s[i + 2] == 'A') &&
- (c == false) && (d == false))
- c = true, i += 2;
- else if ((s[i] == 'A') && (s[i + 1] == 'B') && (a == false))
- a = true, i++;
- else if ((s[i] == 'B') && (s[i + 1] == 'A') && (b == false))
- b = true, i++;
- }
- if ((a == true && b == true) ||
- ((c == true || d == true) && (a == true || b == true)))
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- }
Another solutoin is given bellow :
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- char s[100100], *p;
- cin >> s;
- if ((p = strstr(s, "AB")) && strstr(p + 2, "BA"))
- cout << "YES" << endl;
- else if ((p = strstr(s, "BA")) && strstr(p + 2, "AB"))
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- }
No comments