Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Jun, 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;
}

Java




import java.util.*;
import java.io.*;
 
// Java program for the above approach
class GFG{
 
  static 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) {
        System.out.println("YES");
      }
    }
    else {
      ArrayList<String> v1 = new ArrayList<String>();
      for (int i = 0 ; i < S.length() ; i++) {
 
        // character to integer
        int val = (int)S.charAt(i) - (int)'0';
 
        // convert into String
        String a = Integer.toString(val);
        v1.add(a);
      }
      while (M > 0) {
        ArrayList<String> v = new ArrayList<String>();
        for (int i = 0 ; i < v1.size() ; i++) {
          for (int j = 0; j < v1.get(i).length() ; j++) {
 
            int temp = (int)v1.get(i).charAt(j) - (int)'0';
 
            // multiply X into given integer
            temp = temp * X;
 
            v.add(Integer.toString(temp));
          }
        }
        v1 = v;
        M -= 1;
      }
 
      // 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.get(i).length() ; j++) {
          temp+=v1.get(i).charAt(j);
        }
      }
 
      // Compare both Target and temp
      if (temp.equals(Target)) {
        System.out.println("YES");
      }
      else {
        System.out.println("NO");
      }
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
    String S = "1234";
    String Target = "2550525100";
    int X = 5;
    int M = 2;
    makestring(S, Target, X, M);
  }
}
 
// This code is contributed by subhamgoyal2014.

Python3




# Python program to check
# If strings are Equal or not
def 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 or M == 0):
        if (S == Target):
            print("YES")
    else:
        v1 = []
        for i in range(len(S)):
 
            # character to integer
            val = ord(S[i]) - ord('0')
 
            # convert into string
            a = str(val)
            v1.append(a)
        while (M):
            v = []
            for i in range(len(v1)):
                for j in range(len(v1[i])):
 
                    temp = ord(v1[i][j]) - ord('0')
 
                    # multiply X into given integer
                    temp = temp * X
 
                    v.append(str(temp))
            M -= 1
            v1 = v.copy()
 
        # Store all character from vector
        # of string into temp string
        temp = ""
        for i in range(len(v1)):
            for j in range(len(v1[i])):
                temp += v1[i][j]
 
        # Compare both Target and temp
        if (temp == Target):
            print("YES")
        else:
            print("NO")
 
# Driver Code
S = "1234"
Target = "2550525100"
X = 5
M = 2
 
makestring(S, Target, X, M)
 
# This code is contributed by shinjanpatra

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)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!