Adding Spaces to a String | Straight Forward Easy | Leetcode 2109 | codestorywithMIK

preview_player
Показать описание
This is the 50th Video of our Playlist "Strings" by codestorywithMIK

In this video we will try to solve an easy string problem : Adding Spaces to a String | Straight Forward Easy | Leetcode 2109 | codestorywithMIK

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 : Adding Spaces to a String | Straight Forward Easy | Leetcode 2109 | codestorywithMIK
Company Tags : will update later

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

Summary :
The function addSpaces takes a string s and an array spaces containing indices where spaces need to be inserted. The approach is:

Two Pointers:

One pointer (i) iterates through each character of the string s.
Another pointer (j) traverses the spaces array to check if the current index i matches any index in spaces.
Inserting Spaces:

If i matches the current space index (spaces[j]), a space is appended to the result, and the pointer j is incremented.
Appending Characters:

The current character s[i] is appended to the result, whether or not a space was added.

✨ Timelines✨
00:00 - Introduction

#MIK #mik #Mik
#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
Рекомендации по теме
Комментарии
Автор

sir in editorial this problem was solved using stringstream, it would be great if you explain a little bit about stringstream in next potd video.

Anonymous-bgif
Автор

Dada, Can you please make a video on Sieve of eratosthenes?

JagannathDebGunin
Автор

Please cover yesterday’s CF contest’s question C

cabbi
Автор

Was able to solve today's question by writing code into a story, just as you taught us, Mik Sir❤
P.S.: Sir, please check your mail and revert back if possible.

hellsterkun
Автор

class Solution {
public:
string addSpaces(string s, vector<int>& spaces) {
int n = spaces.size();
for(int i=0 ; i<n ; i++)
{
s.insert(spaces[i]+i, " ");
}
return s;
}
};

65/66 Test Cases passed, TLE for only 1 test case

best
Автор

please make video on manacher algorithm for longest palindrome string and also make video on z algorithm

HumanBeing-mx
Автор

khud se kar liya
but iska runtime kaafi high tha

class Solution {
public String addSpaces(String s, int[] spaces) {
StringBuilder sb = new StringBuilder(s);
int count = 0;
for(int x : spaces){
sb.insert(x+count, ' ');
count++;
}
return sb.toString();
}
}

aizadiqbal
Автор

bhaiya can u make a video to find no of nodes at a distance k from given node in tree

christopherhenry
Автор

bhaya isn't like using the + while concatinating the string takes much space there ?

CHANDANKUMAR-ckis
Автор

my code
string addSpaces(string s, vector<int>& spaces) {
int j=0;
string ans="";

for(int i=0;i<s.length();i++){

if(j<spaces.size() && spaces[j]==i){
ans+=' ';

j++;

}
ans+=s[i];

}
return ans;
}
};

AryanVats
Автор

Esa same code mene java me Kiya toh tle aa raha hai

paragbansal
Автор

Bhaiya I think Time complexity would be just O(n) because we are using if condition which is O(1).❤

jeehub