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

• 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 ``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 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 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 v1 = ``new` `ArrayList();``      ``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 v = ``new` `ArrayList();``        ``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

 ``

Output

`YES`

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

My Personal Notes arrow_drop_up