Check if a String can be Converted to Another by Inserting Character Same as both Neighbours


In this problem, we will check whether we can convert the string alpha1 to alpha2 by inserting the same characters between any two same characters of the given string.

We will use the run length encoding algorithm to solve the problem, which counts the frequency of the contiguous characters.

Problem statement − We have given two strings named alpha1 and alpha2. We need to check whether we can convert the string alpha1 to alpha2 by performing an unlimited number of below operations.

  • Choose any index, and the character at the current and previous index is the same, insert the same character between them.

Sample examples

Input

alpha1 = "pqqrrs"; alpha2 = "pqqqqrrrs";

Output

Yes

Explanation − We can insert two ‘q’ between the 1st and 2nd (0−based indexing) index. Also, we can insert 1 ‘r’ between the 3rd and 4th index to make the alpha1 and alpha2 strings same.

Input

alpha1 = "mnno"; alpha2 = "mnnol";

Output

No

Explanation − We can’t insert ‘l’ in the alpha1 string as no two adjacent ‘l’ exist in the alpha1 string.

Input

alpha1 = "bcvfgdfgd"; alpha2 = "mnnol";

Output

No

Explanation − The length of alpha2 is smaller than the length of alpha1 string. So, we can’t convert the alpha1 to alpha2 string by inserting characters.

Approach 1

We will use the run length encoding algorithm. It traverses the string and counts the frequency of the continuous characters.

For strings alpha1 = "pqqrrs", and alpha2 = "pqqqqrrrs", we get the below outputs using the run length encoding algorithm.

Run1 = {(‘p’, 1), (‘q’, 2), (‘r’, 2), (‘s’, 1)}

Run2 = {(‘p’, 1), (‘q’, 4), (‘r’, 2), (‘s’, 1)}

If we generalize it, we can see the following results.

Run1 = {(p1, m1), (p2, m2), (p3, m3) …. (pX, mX)}

Run2 = {(q1, n1), (q2, n2), (q3, n3) …. (qY, qY)}

If Run1 and Run2 follow the conditions below, we can convert alpha1 to alpha2.

  • X == Y, representing the same number of contiguous characters.

  • p1 == q1, p2 == q2, … pX == qY, representing same characters.

  • It should follow mi == ni or mi < ni and mi > 1, as we can only insert a character if contiguous characters have a frequency more than 2.

Algorithm

Step 1 − Create a list1 and list2 to store the pair of characters and integers.

Step 2 − Execute the encodeStrings() function to encode the strings and store the output in the list1 and list2.

Step 2.1 − In the encodeStrings() function, initialize the ‘cnt’ with 1 to store the count of the current contiguous character.

Step 2.2 − Traverse the string from the 2nd character.

Step 2.3 − If the current character is not the same as the previous character, insert the pair of characters and ‘cnt’ in the list and reinitialize the ‘cnt’ with 0.

Step 2.4 − Increment ‘cnt’ by 1.

Step 2.5 − Once loop iterations are completed, insert the last character with the ‘cnt’ value in the list.

Step 3− If the size of the encoded list is not the same, return false.

Step 4 − Start traversing both lists. If characters at the same index are not the same, return false.

Step 5 − If the frequency value of the character at the current index is not the same, return false.

Step 6 − If the frequency value in list1 is not smaller than list2 or smaller than 2, return false.

Step 7 − At last, return true from the function.

Example

#include <bits/stdc++.h>
using namespace std;

void encodeStrings(string alpha, vector<pair<char, int>> &list) {
    int cnt = 1;
    for (int p = 1; p < alpha.length(); p++) {
        // When we find adjacent different characters
        if (alpha[p] != alpha[p - 1]) {
            list.push_back({alpha[p - 1], cnt});
            cnt = 0;
        }
        cnt++;
    }
    list.push_back({alpha.back(), cnt});
}
bool ConvertStrings(string alpha1, string alpha2) {
    vector<pair<char, int>> list1, list2;
    encodeStrings(alpha1, list1);
    encodeStrings(alpha2, list2);
    // If size of lists are not same
    if (list1.size() != list2.size()) {
        return false;
    }
    for (int p = 0; p < list1.size(); p++) {
        // Validate second condition
        if (list1[p].first != list2[p].first) {
            return false;
        }
        // Validate third condition
        if (!(list1[p].second == list2[p].second || (list1[p].second < list2[p].second && list1[p].second >= 2))) {
            return false;
        }
    }
    return true;
}
int main() {
    string alpha1 = "pqqrrs";
    string alpha2 = "pqqqqrrrs";
    bool res = ConvertStrings(alpha1, alpha2);
    if (res) {
        cout << "YES, It is possible to convert string alpha1 to alpha2." << endl;
    } else {
        cout << "NO, It is not possible to convert string alpha1 to alpha2." << endl;
    }
}

Output

YES, It is possible to convert string alpha1 to alpha2.

Time complexity − O(P + Q), where P is the length of alpha1, and Q is the length of alpha2.

Space complexity − O(M + N) to store the encoded list.

The run length encoding algorithm is very useful when we need to solve any problem based on the frequency of the continuous characters of the given string.

Updated on: 17-Jul-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements