String Division

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 abcd;
    for (int i = 1i < ni++)
    {
        for (int j = i + 1j < nj++)
        {
            for (int k = j + 1k < nk++)
            {
                a = s.substr(0i);
                b = s.substr(ij - i);
                c = s.substr(jk - j);
                d = s.substr(kn - 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";
    else
        cout << "NO\n";

    return 0;
}


No comments:

Post a Comment