Question: You are given two strings, S and T , both of length N . Both strings are made up of digits. In a single move,

You are given two strings, S and T, both of length N. Both strings are made up
of digits. In a single move, you can select an index K within the range [0..N-2]
and increase the digits S[K] and S[K+1] by 1. If a digit 9 is increased, it
becomes 0.
Is it possible to transform S into T by performing some (possibly zero)
number of such moves? If so, what is the minimum number of moves
needed?
Write a function:
int solution(string &S, string &T);
that, given strings S and T, each of length N, returns the minimum number of
moves needed to transform S into T. If it is not possible to obtain T in this
way, return -1.
Examples:
For S="13471" and T="59604", the function should return 9.
A valid sequence of moves could be as follows:
choose index 1 and increase S[1] and S[2] two times. After these moves,
S becomes "15671".
choose index 3 and increase S[3] and S[4] three times. After these
moves, S becomes "15604".
 You are given two strings, S and T, both of length

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!