The motivation part is so good. I love it. Thanks man for adding it in your videos. And I got the idea in the middle of the video (I skipped brute force) and coded on my own. THANK YOU SO MUCH
Hi MIK , thanks for making this video. This was my approach using a sliding window with circular array concept and passed 142 test cases 10 were remaining but couldn't find the issue.class Solution { public: int takeCharacters(string s, int k) { int n = s.length();
// Step 1: Count the total occurrences of 'a', 'b', and 'c' in the string unordered_map total_count; for (char c : s) { total_count[c]++; }
// If we don't have enough of any character, return -1 if (total_count['a'] < k || total_count['b'] < k || total_count['c'] < k) { return -1; } // Step 2: Sliding window with circular logic unordered_map window_count; int left = 0, right = 0; int result = n + 1; // Initialize result as a large number // Step 3: Try taking characters from the right end while (right < 2 * n) { // We loop until we have gone through two full passes (wrap around) window_count[s[right % n]]++; // Circular access using modulo right++; // Step 4: Once we have enough 'a', 'b', and 'c', start shrinking from the left while (window_count['a'] >= k && window_count['b'] >= k && window_count['c'] >= k) { result = min(result, right - left); // Calculate the window size // Try to shrink the window from the left window_count[s[left % n]]--; // Circular access using modulo if (window_count[s[left % n]] == 0) { window_count.erase(s[left % n]); } left++; } } // After processing the whole string, check the result return result == n + 1 ? -1 : result; } };
Hi MiK can you please make a video explaining the leetcode problem 2539. It's quite confusing or can be said to be a big problem. Was recently asked by Swiggy. PS: Not a single video on TH-cam of it 🥲
class Solution { public: void solve(string& s,int i,int j,int& count,vector& store,int k,int n){ if(store[0]>=k && store[1]>=k && store[2]>=k){ count = min(count,store[0]+store[1]+store[2]); return; } if(i=0){ store[s[j]-'a']++; solve(s,i,j-1,count,store,k,n); store[s[j]-'a']--; } else{ count=-1; return; } } int takeCharacters(string s, int k) { int n = s.size(); int i=0; int j = n-1; int count = INT_MAX; vectorstore(3,0); solve(s,i,j,count,store,k,n); return count; } }; sir ye to recursion wala sol. to tle chal gaya 😓ye code sahi hai ya nahi ?
i could write this much only lol -> class Solution { public: int takeCharacters(string s, int k) { int n = s.length(); unordered_map charCount; for (char &ch : s) { charCount[ch]++; } for (int i = 0; i < charCount.size(); i++) { if (i.second < k) { return -1; } } } };
} int takeCharacters(string s, int k) { int i=0,j=s.size()-1; int minstep=INT_MAX; solve(s,k,i,j,minstep,0,0,0); if(minstep==INT_MAX){ return -1; } return minstep; } }; Recursion Code Getting MLE But AMAZING TO CODE🤎
I paused the video and tried with rec : class Solution { public int takeCharacters(String s, int k) { int[] freq = new int[3]; for(char ch: s.toCharArray()){ freq[ch - 'a']++; } for(int n : freq){ if(n < k) return -1; } return sol(0, s.length() - 1, s, new int[3],k ); } private int sol(int start, int end, String s, int[] freq, int k ){ if(freq[0] >= k && freq[1] >= k && freq[2] >= k){ return 0; //no more moves needed } if(start > end){ //invalid path to cover as other way we have covered already //return invalid data and as we are comparing min, return infi return Integer.MAX_VALUE/2; } freq[s.charAt(start) - 'a']++; int left = 1 + sol(start + 1, end,s, freq, k); freq[s.charAt(start) - 'a']--; freq[s.charAt(end) - 'a']++; int right = 1 + sol(start, end - 1,s, freq, k); freq[s.charAt(end) - 'a']--; return Math.min(left, right); } } This is giving TLE, even if i add cache here(memo) , i can not excute this in O(n) times so i need to try sliding window.
"you may take either the leftmost character of s, or the rightmost character of s." although your solution is correct, this is being violated i feel. Why cant we keep one pointer at 0th index and other at s.size()-1
Jb first time probelm dekha to i was like ye kya ques h 😢 Don't know but jese bhaii ki awaz suno ek motivation sa feel ata h 🎉❤ #Thankyou #MIK_Bhai 😅
The motivation part is so good. I love it. Thanks man for adding it in your videos.
And I got the idea in the middle of the video (I skipped brute force) and coded on my own. THANK YOU SO MUCH
Hi MIK , thanks for making this video. This was my approach using a sliding window with circular array concept and passed 142 test cases 10 were remaining but couldn't find the issue.class Solution {
public:
int takeCharacters(string s, int k) {
int n = s.length();
// Step 1: Count the total occurrences of 'a', 'b', and 'c' in the string
unordered_map total_count;
for (char c : s) {
total_count[c]++;
}
// If we don't have enough of any character, return -1
if (total_count['a'] < k || total_count['b'] < k || total_count['c'] < k) {
return -1;
}
// Step 2: Sliding window with circular logic
unordered_map window_count;
int left = 0, right = 0;
int result = n + 1; // Initialize result as a large number
// Step 3: Try taking characters from the right end
while (right < 2 * n) { // We loop until we have gone through two full passes (wrap around)
window_count[s[right % n]]++; // Circular access using modulo
right++;
// Step 4: Once we have enough 'a', 'b', and 'c', start shrinking from the left
while (window_count['a'] >= k && window_count['b'] >= k && window_count['c'] >= k) {
result = min(result, right - left); // Calculate the window size
// Try to shrink the window from the left
window_count[s[left % n]]--; // Circular access using modulo
if (window_count[s[left % n]] == 0) {
window_count.erase(s[left % n]);
}
left++;
}
}
// After processing the whole string, check the result
return result == n + 1 ? -1 : result;
}
};
Just search kar rh tha aaj ka potd ka and here we go
Hatsoff mik bhaiya for motivation at the strt the intuition for the problem thnku so much 🫡
Radhe Radhe ❤
I have a interview today mik bro wish me luck and pls give some tips for interview ❤
All the best bro. Which company ?
Confident raho bas.
All the best bhai
All the very best buddy!
Thanks bro ❤
@@gui-codes deloite
like your quotation, i mean motivation. also solution as well.
Mujhe yeh brute force hi laga tha aur laga ki shayad dp hai 😅😅
Thanks for the approach, it was easy to code after understanding the approach
very good question
just brilliant
Fantastic explanation and the solution
#optimization #codetostorywithmik
while(j
Thank you bhaiya
I have used Binary search + sliding window
great explaination
You are too good MIK
119/142 test case passed by own
132 by own
Hi MiK can you please make a video explaining the leetcode problem 2539. It's quite confusing or can be said to be a big problem. Was recently asked by Swiggy.
PS: Not a single video on TH-cam of it 🥲
Bhai ye to leetcode premium problem hai not accessible by all
@dhruvchopra26 I know but it was asked in Swiggy coding round that's why I asked
Bhaiya tcs codevita ke previous year question pe vedios bna do plz❤ khi se ache resources nhi mil rhe
Instead why can't we apply dp (index,counta,countb ,count c, minutes) and return minutes 5d dp but less than 10 ^ 8 so will run
Why dp cannot be used for this question. Why not converting recursion to memoization
Constraints are 1e5. Will Give TLE.
First 🎉❤
second 😁
Hi, did someone do the code which told to do it in the beginning (with recusion), please post it.
class Solution {
public:
void solve(string& s,int i,int j,int& count,vector& store,int k,int n){
if(store[0]>=k && store[1]>=k && store[2]>=k){
count = min(count,store[0]+store[1]+store[2]);
return;
}
if(i=0){
store[s[j]-'a']++;
solve(s,i,j-1,count,store,k,n);
store[s[j]-'a']--;
}
else{
count=-1;
return;
}
}
int takeCharacters(string s, int k) {
int n = s.size();
int i=0;
int j = n-1;
int count = INT_MAX;
vectorstore(3,0);
solve(s,i,j,count,store,k,n);
return count;
}
};
sir ye to recursion wala sol. to tle chal gaya 😓ye code sahi hai ya nahi ?
i could write this much only lol -> class Solution {
public:
int takeCharacters(string s, int k) {
int n = s.length();
unordered_map charCount;
for (char &ch : s) {
charCount[ch]++;
}
for (int i = 0; i < charCount.size(); i++) {
if (i.second < k) {
return -1;
}
}
}
};
#Recursion
class Solution {
public:
void solve(string s,int k,int i,int j,int &minstep,int a,int b,int c){
if(a>=k && b>=k && c>=k){
int val=(s.size()-j)+i-1;
minstep=min(minstep,val);
return ;
}
if(i>j){
return ;
}
if(s[i]=='a'){
a++;
solve(s,k,i+1,j,minstep,a,b,c);
a--;
}
if(s[i]=='b'){
b++;
solve(s,k,i+1,j,minstep,a,b,c);
b--;
}
if(s[i]=='c'){
c++;
solve(s,k,i+1,j,minstep,a,b,c);
c--;
}
if(s[j]=='a'){
a++;
solve(s,k,i,j-1,minstep,a,b,c);
a--;
}
if(s[j]=='b'){
b++;
solve(s,k,i,j-1,minstep,a,b,c);
b--;
}
if(s[j]=='c'){
c++;
solve(s,k,i,j-1,minstep,a,b,c);
c--;
}
}
int takeCharacters(string s, int k) {
int i=0,j=s.size()-1;
int minstep=INT_MAX;
solve(s,k,i,j,minstep,0,0,0);
if(minstep==INT_MAX){
return -1;
}
return minstep;
}
};
Recursion Code Getting MLE But AMAZING TO CODE🤎
Have you uploaded its explanation in Leet code discussion ? if yes then can you say your username on leetcode?
____loki
you can refer .
how u r able to find solution bro.. i am not able to solve it ny self.. i have to look solution for each problem..
bhaiya aap ye konsa app use karte ho ipad pe hame samzane ke liye pls bta do @codestorywithMIK
It’s the default NOTES app in ipad
I hate this problem..
Knuth algo ? Just reminding
I paused the video and tried with rec :
class Solution {
public int takeCharacters(String s, int k) {
int[] freq = new int[3];
for(char ch: s.toCharArray()){
freq[ch - 'a']++;
}
for(int n : freq){
if(n < k) return -1;
}
return sol(0, s.length() - 1, s, new int[3],k );
}
private int sol(int start, int end, String s, int[] freq, int k ){
if(freq[0] >= k && freq[1] >= k && freq[2] >= k){
return 0; //no more moves needed
}
if(start > end){
//invalid path to cover as other way we have covered already
//return invalid data and as we are comparing min, return infi
return Integer.MAX_VALUE/2;
}
freq[s.charAt(start) - 'a']++;
int left = 1 + sol(start + 1, end,s, freq, k);
freq[s.charAt(start) - 'a']--;
freq[s.charAt(end) - 'a']++;
int right = 1 + sol(start, end - 1,s, freq, k);
freq[s.charAt(end) - 'a']--;
return Math.min(left, right);
}
}
This is giving TLE, even if i add cache here(memo) , i can not excute this in O(n) times
so i need to try sliding window.
"you may take either the leftmost character of s, or the rightmost character of s."
although your solution is correct, this is being violated i feel.
Why cant we keep one pointer at 0th index and other at s.size()-1
Note that sliding window is finding the window of not_deleted characters. Hence starting from i = 0 and j = 0 and then moving forward
@@codestorywithMIK okay, understood...thankssss
class Solution {
public:
int takeCharacters(string s, int k) {
int n=s.length();
vectorfreq(3,0);
for(auto ch:s){
freq[ch-'a']++;
}
if(freq[0]
Yep yours is a cleaner one 👌❤️
This is another good way.
Thank you for sharing with all of us
@@codestorywithMIK Thank you for the appreciation❤