Kevin has a string S consisting of N lowercase English letters.
Kevin wants to split it into 4 pairwise different non-empty parts. For example, string "happynewyear" can be splitted into "happy", "new", "ye" and "ar". He can't delete any characters or change the order of the characters.
Help Kevin and find if there exist at least one possible splitting.
Example:
Input: s = "ababca"
Output: YES
Approach
C++
#include <bits/stdc++.h>using namespace std;bool stringDivision(string s){int n = s.length();if (n >= 10){return true;}if (n < 4){return false;}string a, b, c, d;for (int i = 1; i < n; i++){for (int j = i + 1; j < n; j++){for (int k = j + 1; k < n; k++){a = s.substr(0, i);b = s.substr(i, j - i);c = s.substr(j, k - j);d = s.substr(k, n - k);if (a != b && a != c && a != d && b != c && b != d && c != d){return true;}}}}return false;}int main(){string s = "ababca";if (stringDivision(s))cout << "YES\n";elsecout << "NO\n";return 0;}
No comments:
Post a Comment