Leetcode 796. Rotate String (Python)

describe

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	
Copy the code

Example 2:

Input: A = 'abcde', B = 'abced'
Output: false
Copy the code

Note:

A and B will have length at most 100.
Copy the code

parsing

According to the question, is the judgment after the shift operation of A and B are equal, first determine whether the same number of characters and, if they are equal to A len (A) time shift operation, each operation whether its and B are equal, if equal returns True, If len(A) is not equal after traversing len(A) times, return False.

answer

class Solution(object): def rotateString(self, A, B): """ :type A: str :type B: str :rtype: bool """ a = collections.Counter(A) b = collections.Counter(B) if a ! = b: return False n = len(A) while n >= 0: if A == B: return True else: n -= 1 A = A[1:]+A[0] return FalseCopy the code

The results

The node is linked to the link in the linked list. Memory Usage: The linked list is given in the Python online submissions for the Rotate String.Copy the code

Original link: leetcode.com/problems/ro…

Your support is my biggest motivation