코딩테스트/프로그래머스[모든문제Lv.0]
문자열 밀기 ☑️
두퍼
2023. 3. 31. 12:54
1. Calrifying Question
- 한번에 하나씩만 옆으로 밀어서 만들 수 있다는 것?
- 대문자이면 다른 언어로 생각하는 건가? 일단 문제에 모두 소문자로 이루어져있다고 되어있음
- 미는 것을 어떻게 구현 할 것인가
2. Optimized solution
- 다시 원문 워드로 돌아올 때까지만 확인해야 한다는 점
- 밀어주기 구현
- 하나씩 다 땡기는....
3. Code
[내 시도 >> 완전 멸망]
def solution(A, B):
answer = 0
listA = list(A)
cnt = len(listA)
print(cnt)
for j in range(cnt):
for i in range(cnt):
if i == cnt - 1:
listA[0] = listA[-1]
else:
listA[i + 1] = listA[i]
answer += 1
print(listA)
if listA == list(B):
return answer
else:
return -1
[이런 식으로 되버리......]
['h', 'h', 'l', 'l', 'o']
['h', 'h', 'h', 'l', 'o']
['h', 'h', 'h', 'h', 'o']
['h', 'h', 'h', 'h', 'h']
['h', 'h', 'h', 'h', 'h']
['h', 'h', 'h', 'h', 'h']
⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️ 블로그 도움⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️
A = A[-1] + A[:-1] >> 이런 생각 부럽
def solution(A, B):
result = 0
while result != len(A):
if A == B:
return result
A = A[-1] + A[:-1]
result += 1
return -1