Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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".
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

What is the average age of members of your key public?

Answered: 1 week ago

Question

How likely is this public to act on information it receives?

Answered: 1 week ago

Question

What does this public think about your organization?

Answered: 1 week ago