Rotate String - String - Easy - LeetCode
💻 coding

Rotate String - String - Easy - LeetCode

1 min read 126 words
1 min read
ShareWhatsAppPost on X
  • 1A shift on string A involves moving the leftmost character to the rightmost position.
  • 2The function returns True if string A can become string B after some shifts.
  • 3The time complexity of the solution is O(n) and the space complexity is O(n).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"A shift on string A involves moving the leftmost character to the rightmost position."

Rotate String - String - Easy - LeetCode

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 a length of at most 100.

public class Solution {
 public bool RotateString(string A, string B) {
 if(A.Length != B.Length){
 return false;
 }
 
 var str = A + A;
 return str.Contains(B);
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 17 October 2020 · 1 min read · 126 words

Part of AskGif Blog · coding

You might also like

Rotate String - String - Easy - LeetCode | AskGif Blog