DESCRIPTION:
Write a function that receives two strings and returns n, where n is equal to the number of characters we should shift the first string forward to match the second. The check should be case sensitive.
For instance, take the strings "fatigue" and "tiguefa". In this case, the first string has been rotated 5 characters forward to produce the second string, so 5 would be returned.
If the second string isn't a valid rotation of the first string, the method returns -1.Examples:
"coffee", "eecoff" => 2
"eecoff", "coffee" => 4
"moose", "Moose" => -1
"isn't", "'tisn" => 2
"Esham", "Esham" => 0
"dog", "god" => -1
def shifted_diff(second, first):
if len(first)!=len(second):
return -1
count=0
for i in range(len(first)):
if first!=second:
first=first[1::]+first[0]
count+=1
else:
return count
return -1
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
What century is it? (0) | 2022.11.04 |
---|---|
Arrh, grabscrab! (0) | 2022.11.02 |
Coding Meetup #8 Will all continents be represented? (0) | 2022.11.02 |
N-th Fibonacci (0) | 2022.11.02 |
Srot the inner ctonnet in dsnnieedcg oredr (0) | 2022.11.01 |