Rotate String | Something to learn | Leetcode 796 | codestorywithMIK

preview_player
Показать описание
This is the 57th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve an easy string based Problem : Rotate String | Something to learn | Leetcode 796 | codestorywithMIK
NOTE - You will learn a few good things in this video which will help you in future qns.

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Rotate String | Something to learn | Leetcode 796 | codestorywithMIK
Company Tags : LinkedIn

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Brute Force Approach (Iterative Rotations):

Logic: Rotate the string s one character at a time and check if it matches goal.
Steps:
Use a loop to rotate s up to m times (where m is the length of s).
For each rotation, shift s left by one character.
If s matches goal at any step, return true.
Time Complexity: O(m2) (where m is the length of s), due to rotating the string and comparing it m times.
Space Complexity: O(1) if modifying s in place.
Optimized Approach (Double String Method):

Logic: Concatenate s with itself (i.e., s + s) and check if goal is a substring of this concatenated string.
Steps:
Check if the lengths of s and goal are equal.
Create s + s and use .contains(goal) in Java or .find(goal) != string::npos in C++ to see if goal is a substring.
If goal is found, return true; otherwise, return false.
Time Complexity: O(m), as substring search in s + s is linear.
Space Complexity: O(m) due to the space needed for s + s.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

I want to be like you MIK.
travelling + studying + desciplied etc. you manage everything well.

souravjoshi
Автор

Binge watching all your playlists. They are too good.
Thanks a lot

FanIQQuiz
Автор

yes sir i remember 2nd approach aapne batai thi purnai video me, btw thank you for great explanation❤❤❤❤

bhupendrakalal
Автор

I used this approach, it's cool that it worked..

class Solution {
public boolean rotateString(String s, String goal) {
String combined = s + s;
if(goal.length() < s.length()){
return false;
}
if(combined.indexOf(goal) != -1){
return true;
}

return false;
}
}

aizadiqbal
Автор

Thanks a lot MIK. Always learning from you.

gui-codes
Автор

love you. thanks a lot mik for improving my problem solving skills

EB-otuu
Автор

How do you think, Mindblowing...
Love you bro ❤

shivambhaskar
Автор

You can use String.indexOf method to find the substring in a given string.

*Java Code*
if(s.length() != goal.length()) return false;
String t = s + s;
if(t.indexOf(goal) != -1) return true;
return false;

satyasanjay
Автор

class Solution {
public:
bool rotateString(string s, string goal) {
false;
string check=s+s;
if(check.find(goal)==-1){
return false;
}
return true;
}
};

Coder_Buzz
Автор

class Solution {
public:
bool rotateString(string s, string goal) {
if(s.size() > goal.size()) return false;
string ans = s;
ans += s;
if(ans.find(goal) < ans.length()) return true;
return false;
}
};

Abhishek
Автор

when you will rotate it by 2 it will be "cdefab" not the thank u for the video....got to learn so many things...

adityaasthana
Автор

class Solution {
public:
bool rotateString(string s, string goal) {
int n = s.size();
for(int i = 0; i<s.length(); i++){
char ch = s[0];
if(s == goal) return true;
s.push_back(ch);
s.erase(0, 1);
n--;
if(n == 0)break;
}
return false;
}
void checkString(string s){
for(int i = 0; i<s.length(); i++){
char ch = s[]
}
}
};
Time Complexity O(n)
Space Complexity O(1)

dipanshuraj
Автор

bool rotateString(string s, string goal) {
if(s.size()>goal.size()) return false;
string str=s+s;
if(str.find(goal)!=string ::npos) return true;
return false;
} My solution in c++

ShashankShekharShukla-xwrc
Автор

Please make a video on leetcode 3337. It was the 5th problem in leetcode weekly 421. for me it is very hard to understand that problem from leetcode comments. Also there is no proper explanation of that problem on yt + It is from an advanced topic (matrix exponentiation)

jashanpreet.
Автор

check this out


class Solution {
public boolean rotateString(String s, String goal) {

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

return (s+s).contains(goal);

}
}

kyogesh
Автор

not so optimized but here's my solution

class Solution {
public:
bool rotateString(string s, string goal) {
int n=s.length();
string newS=s;
string out="";
while(out!=s){
out=newS[n-1]+newS.substr(0, n-1);
if(out==goal){
return true;
}
newS=out;
}

return false;
}
};

adritaadi
Автор

Bhaiya left shift mein n^2 lag raha tha, isiliye mene right shift Kiya Tha, aur after one right shift check Kar raha hu, ki same hai ki nhi, aur solved ho gaya

RishabhChatterjee-fggz
Автор

Ye yaad kaise rahega ...agar in future pta v lg jaye esa kuch use krna h

-cse-csmohitkumarmandal
Автор

Hii, aap kon sa writing pad use krte ho? any suggestions?

ClearYourConcepts-zj
Автор

What Software, You Use For Making Videos ?

MathBytes
join shbcf.ru