컴퓨터공학/LeetCode 1000
[LeetCode] 796. Rotate String
saurus2
2022. 11. 23. 07:24
796. Rotate String
Easy
Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
A shift on s consists of moving the leftmost character of s to the rightmost position.
- For example, if s = "abcde", then it will be "bcdea" after one shift.
Example 1:
Input: s = "abcde", goal = "cdeab"
Output: true
Example 2:
Input: s = "abcde", goal = "abced"
Output: false
Constraints:
- 1 <= s.length, goal.length <= 100
- s and goal consist of lowercase English letters.
문제 풀이
- 문자를 한 글자씩 뒤로 보내서 goal에 들어있는 문자열과 같은지 확인하는 문제이다.
- 리스트에 문자열을 넣고 앞에서 글자를 하나씩 뒤로 옮겨서 확인한 후 답을 반환한다.
- 글자를 모두 이동 시켰지만 같은 문자열로 바뀌지 않는다면 마지막에 False를 반환한다.
소스 코드
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
lst = list(s)
for i in range(len(s)):
if ''.join(lst) == goal:
return True
lst.append(lst.pop(0))
return False