Skip to main content

Command Palette

Search for a command to run...

Strings - Rotate String

Published
1 min read
Strings - Rotate String
N

I am Nirbhay Singh , I am starting this blog to document my coding journey of becoming a software developer and to get my first $ 100k offer .

Rotate String

https://leetcode.com/problems/rotate-string/description/

Hint:

Use 'substr' method to change the string without actually modifying the string.

Code:

class Solution {
public:
    bool rotateString(string s, string goal) {
        if(s.size()!=goal.size()){
            return false;
        }

        int n = s.size();

        for(int i=0;i<n;i++){
            string temp = s.substr(i+1,n)+s.substr(0,i+1);
            if(temp==goal){
                return true;
            }
        }

        return false;
    }
};

More from this blog

Daily Code by Nirbhay

74 posts

Hey, this is Nirbhay. I started this blog to document my journey of learning to code and get my first $100k offer. I'll be sharing the things related to DSA, backend development, devops and many more.