Techno Blender
Digitally Yours.

Split Strings into two Substrings whose distinct element counts are equal

0 39


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string S of length N, the task is to check if a string can be split into two non-intersecting strings such that the number of distinct characters are equal in both.

Examples:

Input: N = 6, S = “abccba”
Output: True
Explanation: Splitting two strings as “abc” and “cba” has 3 distinct characters each.

Input: N = 5, S = “aacaa”
Output: False
Explanation: Not possible to split it into two strings of the same distinct characters.

Approach: To solve the problem follow the below intuition:

The intuition behind solving the problem of checking if a string can be split into two parts such that both parts have the same number of distinct letters to keep track of the frequency of the characters in the string and compare it with the frequency of characters in the substrings as we iterate through the string.

Follow the below steps to solve the problem:

  • Starts by storing the frequency of each character in the input string in an array called freq of size 26.
  • Then, creates another array temp of size 26 to store the frequency of each character in a portion of the input string. 
  • Then iterates over the input string and in each iteration reduce the frequency of the current character in the freq array and increase the frequency of the current character in the temp array.
  • After that, initializes two variables cnt1 and cnt2 to 0 and iterate over both the freq and temp arrays to count the number of non-zero values in each array.
  • If cnt1 and cnt2 are equal, it means that the input string can be split into two parts such that they have the same number of distinct letters, and the function returns True
  • If it’s not possible to split the string into two parts such that they have the same number of distinct letters, the function returns False.

Below is the implementation of the above approach. 

C++

  

#include <bits/stdc++.h>

using 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 main()

{

    string s = "abccba";

    int n = s.size();

  

    

    if (isGoodSplit(s, n)) {

        cout << "True";

    }

    else {

        cout << "False";

    }

    return 0;

}

Time Complexity: O(N*26), where N represents the size of the given string and 26 for the inner loop.
Auxiliary Space: O(N), where N represents the size of the freq and temp array.


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given a string S of length N, the task is to check if a string can be split into two non-intersecting strings such that the number of distinct characters are equal in both.

Examples:

Input: N = 6, S = “abccba”
Output: True
Explanation: Splitting two strings as “abc” and “cba” has 3 distinct characters each.

Input: N = 5, S = “aacaa”
Output: False
Explanation: Not possible to split it into two strings of the same distinct characters.

Approach: To solve the problem follow the below intuition:

The intuition behind solving the problem of checking if a string can be split into two parts such that both parts have the same number of distinct letters to keep track of the frequency of the characters in the string and compare it with the frequency of characters in the substrings as we iterate through the string.

Follow the below steps to solve the problem:

  • Starts by storing the frequency of each character in the input string in an array called freq of size 26.
  • Then, creates another array temp of size 26 to store the frequency of each character in a portion of the input string. 
  • Then iterates over the input string and in each iteration reduce the frequency of the current character in the freq array and increase the frequency of the current character in the temp array.
  • After that, initializes two variables cnt1 and cnt2 to 0 and iterate over both the freq and temp arrays to count the number of non-zero values in each array.
  • If cnt1 and cnt2 are equal, it means that the input string can be split into two parts such that they have the same number of distinct letters, and the function returns True
  • If it’s not possible to split the string into two parts such that they have the same number of distinct letters, the function returns False.

Below is the implementation of the above approach. 

C++

  

#include <bits/stdc++.h>

using 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 main()

{

    string s = "abccba";

    int n = s.size();

  

    

    if (isGoodSplit(s, n)) {

        cout << "True";

    }

    else {

        cout << "False";

    }

    return 0;

}

Time Complexity: O(N*26), where N represents the size of the given string and 26 for the inner loop.
Auxiliary Space: O(N), where N represents the size of the freq and temp array.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment