Today, in an interview, I was asked this programming question:
Given a string, w
, following two operations are performed alternatively per day.
- Remove last
m
characters fromw
and prepend them tow
.m
is less than length ofw
. - Remove last
n
characters fromw
and prepend them tow
.n
is less than length ofw
.
Find how many days would it take to get back w
if we start to perform the above operations.
For example,
if w
is abcde
and m
is 2
and n
is 3
, then
original w
: abcde
after m
op, w
: deabc
after n
op, w
: abcde
stop since new string abcde
is same as the original string w = abcde
My approach was the brute force one where I had a loop and was performing both of the above operation and was continuously checking if the new w is same as the original w. However, this approach is definitely not scalable.
Are all characters of the string distinct?
Not necessarily. $\endgroup$ – user3243499 Jul 8 '19 at 5:14