Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/796.html

796. Rotate String (Easy)

We are given two strings, A and B.

A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true

Example 2:
Input: A = 'abcde', B = 'abced'
Output: false

Note:

  • A and B will have length at most 100.

Companies:
Apple, Microsoft

Solution 1.

  • class Solution {
        public boolean rotateString(String A, String B) {
            return A.length() == B.length() && (B + B).indexOf(A) >= 0;
        }
    }
    
    ############
    
    class Solution {
        public boolean rotateString(String s, String goal) {
            return s.length() == goal.length() && (s + s).contains(goal);
        }
    }
    
  • // OJ: https://leetcode.com/problems/rotate-string/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    public:
        bool rotateString(string s, string goal) {
            return goal.size() == s.size() && (s + s).find(goal) != string::npos;
        }
    };
    
  • class Solution:
        def rotateString(self, s: str, goal: str) -> bool:
            return len(s) == len(goal) and goal in s + s
    
    ############
    
    class Solution(object):
        def rotateString(self, A, B):
            """
            :type A: str
            :type B: str
            :rtype: bool
            """
            if A == B == "":
                return True
            for i in range(len(A)):
                if A[i:] + A[:i] == B:
                    return True
            return False
    
  • func rotateString(s string, goal string) bool {
    	return len(s) == len(goal) && strings.Contains(s+s, goal)
    }
    
  • function rotateString(s: string, goal: string): boolean {
        return s.length === goal.length && (goal + goal).includes(s);
    }
    
    
  • impl Solution {
        pub fn rotate_string(s: String, goal: String) -> bool {
            s.len() == goal.len() && (s.clone() + &s).contains(&goal)
        }
    }
    
    
  • class Solution {
        /**
         * @param String $s
         * @param String $goal
         * @return Boolean
         */
        function rotateString($s, $goal) {
            return strlen($goal) === strlen($s) && strpos(($s.$s), $goal) !== false;
        }
    }
    

All Problems

All Solutions