Description
In the string "hello", push each character one space to the right and move the last character forward to become "ohell". If you define this as pushing a string, complete the solution function so that given the strings A and B as parameters, you have to push A to return the minimum number of times that you have to push B if you can, and push B to return -1 if you can't.
Restrictions
- 0 < length of A = length of B < 100\
- A and B are made up of lowercase letters.
Input/Output
A | B | result |
"hello" | "ohell" | 1 |
"apple" | "elppa" | -1 |
"atat" | "tata" | 1 |
"abc" | "abc" | 0 |
Code
def solution(A, B):
answer = 0
while answer != len(A):
if A == B:
return answer
else:
A = A[-1] + A[:-1]
answer += 1
return -1
Discussion
The key point of this question is to determine the location and the transformation of the string well. It was important to know the slicing.
EX: a = 'hello world'
a[0:5]
result >> 'hello'
In string, couting each alphabets always starts with 0 not 1.
'Coding > Basic Skills(Python)' 카테고리의 다른 글
[Programmers, Level 1] Password Just for Two of us (0) | 2023.06.08 |
---|---|
[Programmers, Level 1] Weird Keyboard (0) | 2023.06.05 |
[Programmers,Level 0] Safe Zone (0) | 2023.04.30 |
[Programmers, Level 0] 캐릭터 좌표(Character Coordination) (0) | 2023.04.23 |
[Programmers, Level 0] OX Quiz (2) | 2023.04.16 |