The Wayback Machine - https://web.archive.org/web/20220302160730/https://www.geeksforgeeks.org/check-if-s-can-be-converted-to-target-in-m-steps-by-replacing-each-digit-with-its-product-with-x/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if S can be converted to Target in M steps by replacing each digit with its product with X

  • Last Updated : 22 Feb, 2022

Given two numeric strings S and Target, and integers X, M. The task is to check if string S can be converted into string Target using the below operation:

  • In one operation it is required to replace each digit in S with its product with X, i.e S[i] = (S[I]*X).
  • The above operation has to be done exactly M times.

Examples:

Input: S = “1234”, Target = “2550525100”, X = 5, M = 2
Output: YES
Explanation:
Initially S=1234
After the 1st, level S becomes =5101520
After the 2nd level, S=2550525100
Thus, we are able to make Target string So print YES
Input: S = “53783”, Target = “2893653”, X = 2, M = 3
Output: NO

 

Approach: This is an implementation-based problem simply traverse from left to right and multiply X in every digit which is present in the original string. Perform this M times with the given string S and check if it is possible to convert original string S into Target string.

Below is the implementation of the above approach: 

C++




// C++ program to check
// If strings are Equal or not
 
#include <bits/stdc++.h>
using namespace std;
 
void makestring(string S, string Target, int X, int M)
{
    // Out of X and M if any one
    // having value zero then just compare
    // original string To Target string
    if (X == 0 || M == 0) {
        if (S == Target) {
            cout << "YES" << endl;
        }
    }
    else {
        vector<string> v1;
        for (int i = 0; i < S.length(); i++) {
 
            // character to integer
            int val = S[i] - '0';
 
            // convert into string
            string a = to_string(val);
            v1.push_back(a);
        }
        while (M--) {
            vector<string> v;
            for (int i = 0; i < v1.size(); i++) {
                for (int j = 0; j < v1[i].size(); j++) {
 
                    int temp = v1[i][j] - '0';
 
                    // multiply X into given integer
                    temp = temp * X;
 
                    v.push_back(to_string(temp));
                }
            }
            v1 = v;
        }
 
        // Store all character from vector
        // of string into temp string
        string temp = "";
        for (int i = 0; i < v1.size(); i++) {
            for (int j = 0; j < v1[i].size(); j++) {
                temp.push_back(v1[i][j]);
            }
        }
 
        // Compare both Target and temp
        if (temp == Target) {
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
}
 
// Driver Code
int main()
{
    string S = "1234";
    string Target = "2550525100";
    int X = 5;
    int M = 2;
    makestring(S, Target, X, M);
    return 0;
}

Javascript




<script>
    // JavaScript program to check
    // If strings are Equal or not
    const makestring = (S, Target, X, M) => {
     
        // Out of X and M if any one
        // having value zero then just compare
        // original string To Target string
        if (X == 0 || M == 0) {
            if (S == Target) {
                document.write("YES<br/>");
            }
        }
        else {
            let v1 = [];
            for (let i = 0; i < S.length; i++) {
 
                // character to integer
                let val = S.charCodeAt(i) - '0'.charCodeAt(0);
 
                // convert into string
                let a = (val).toString();
                v1.push(a);
            }
            while (M--) {
                let v = [];
                for (let i = 0; i < v1.length; i++) {
                    for (let j = 0; j < v1[i].length; j++) {
 
                        let temp = v1[i].charCodeAt(j) - '0'.charCodeAt(0);
 
                        // multiply X into given integer
                        temp = temp * X;
 
                        v.push(temp.toString());
                    }
                }
                v1 = [...v];
            }
 
            // Store all character from vector
            // of string into temp string
            let temp = "";
            for (let i = 0; i < v1.length; i++) {
                for (let j = 0; j < v1[i].length; j++) {
                    temp += v1[i][j];
                }
            }
 
            // Compare both Target and temp
            if (temp == Target) {
                document.write("YES<br/>");
            }
            else {
                document.write("NO<br/>");
            }
        }
    }
 
    // Driver Code
    let S = "1234";
    let Target = "2550525100";
    let X = 5;
    let M = 2;
 
    makestring(S, Target, X, M);
 
    // This code is contributed by rakeshsahni
 
</script>

 
 

Output
YES

 

Time Complexity: O(N* N* M)
Auxiliary Space: O(N)

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!