본문 바로가기
LeetCode

796. Rotate String 문제

by O_x 2022. 7. 7.
796. Rotate String
Easy
194788Add to ListShare

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.

이문제는 약간의 트릭을 사용하여 쉽게 풀수 있다 그 트릭은

s string을 s+s로 하여 s의 원소를 움직여 바꿀수 있는지 확인 할 수 있다.

class Solution {
public:
    bool rotateString(string s, string goal) {
        return s.size() == goal.size() && (s+s).find(goal)!=string::npos;
    }
};

 

 

'LeetCode' 카테고리의 다른 글

415. Add Strings 문제 c++  (0) 2022.07.10
125. Valid Palindrome 문제  (0) 2022.07.10
28. Implement strStr() c++  (0) 2022.07.07
1. Two Sum Leetcode 문제  (0) 2022.07.04
287. Find the Duplicate Number c++ 문제 풀이  (0) 2022.07.03

댓글