Post

812 Rotate String

812 Rotate String

Rotate String image

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.

1
For example, if s = "abcde", then it will be "bcdea" after one shift.

 

Example 1:

1
2
3
**Input:** s = "abcde", goal = "cdeab"
**Output:** true

Example 2:

1
2
3
**Input:** s = "abcde", goal = "abced"
**Output:** false

 

Constraints:

1
2
1 <= s.length, goal.length <= 100
s and goal consist of lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

impl Solution {
    pub fn rotate_string(s: String, goal: String) -> bool {
        // make the string twice and then perform string match using sliding window

        if s.len() != goal.len() {
            return false;
        }

        let mut new_s = s.clone() + &s;
        let mut left = 0;
        let mut right = goal.len();

        while(right < new_s.len()) {
            //print!("{} {}", goal, &new_s[left..right]  );
            if goal == new_s[left..right]{
                return true;
            }
            right += 1;
            left += 1;
        }
        return false;
    }
}



This post is licensed under CC BY 4.0 by the author.