Software Development

Cut up Strings into two Substrings whose distinct factor counts are equal

Cut up Strings into two Substrings whose distinct factor counts are equal
Written by admin


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given a string S of size N, the duty is to verify if a string may be cut up into two non-intersecting strings such that the variety of distinct characters are equal in each.

Examples:

Enter: N = 6, S = “abccba”
Output: True
Rationalization: Splitting two strings as “abc” and “cba” has 3 distinct characters every.

Enter: N = 5, S = “aacaa”
Output: False
Rationalization: Not attainable to separate it into two strings of the identical distinct characters.

Method: To unravel the issue comply with the beneath instinct:

The instinct behind fixing the issue of checking if a string may be cut up into two elements such that each elements have the identical variety of distinct letters to maintain observe of the frequency of the characters within the string and examine it with the frequency of characters within the substrings as we iterate by means of the string.

Comply with the beneath steps to unravel the issue:

  • Begins by storing the frequency of every character within the enter string in an array referred to as freq of measurement 26.
  • Then, creates one other array temp of measurement 26 to retailer the frequency of every character in a portion of the enter string. 
  • Then iterates over the enter string and in every iteration scale back the frequency of the present character within the freq array and improve the frequency of the present character within the temp array.
  • After that, initializes two variables cnt1 and cnt2 to 0 and iterate over each the freq and temp arrays to rely the variety of non-zero values in every array.
  • If cnt1 and cnt2 are equal, it signifies that the enter string may be cut up into two elements such that they’ve the identical variety of distinct letters, and the operate returns True
  • If it’s not attainable to separate the string into two elements such that they’ve the identical variety of distinct letters, the operate returns False.

Beneath is the implementation of the above method. 

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool isGoodSplit(string s, int n)

{

  

    

    

    vector<int> freq(26, 0);

  

    

    for (auto c : s)

        freq++;

  

    

    vector<int> temp(26, 0);

  

    int maxi = 0;

    

    for (int i = 0; i < n; i++) {

  

        

        

        freq[s[i] - 'a']--;

  

        

        

        temp[s[i] - 'a']++;

  

        

        int cnt1 = 0, cnt2 = 0;

  

        

        for (int j = 0; j < 26; j++) {

  

            

            

            if (freq[j])

                cnt1++;

  

            

            

            if (temp[j])

                cnt2++;

        }

        

        

        if (cnt1 == cnt2)

            return true;

    }

    

    return false;

}

  

int fundamental()

{

    string s = "abccba";

    int n = s.measurement();

  

    

    if (isGoodSplit(s, n)) {

        cout << "True";

    }

    else {

        cout << "False";

    }

    return 0;

}

Time Complexity: O(N*26), the place N represents the dimensions of the given string and 26 for the inside loop.
Auxiliary Area: O(N), the place N represents the dimensions of the freq and temp array.

About the author

admin

Leave a Comment