Using binary operation xor makes its highly efficient: public int minChanges(String s) { int ans = 0, i = 0; while (i < s.length()) { ans += s.charAt(i++) ^ s.charAt(i++); // gives 1 when last digit is set and unset in any one position } return ans; }
Thank you so much bhai! Today I exactly copied your style and pattern to code by pausing and matched each and everyline with it!! I'm getting confident with each day because of you!
Thanks MIK for the explanation. The finest explanation of all. Here is the code class Solution { public: int minChanges(string s) { int i = 0, j = 1; int change = 0; while (j < s.size()) { if (s[i] != s[j]) { change++; } i += 2, j += 2; } return change; } }; keep shining as always.
hello mik bhaiya thnku so much second approach mere dimag me aya tha but sure nhi tha me toh and apka potd wala video toh dekhna hi hai if bhale hi merese problem hojaay and and 1st approach se solve kiya apke intution is next level kaffi thought process improve hui hai and second se toh hua hi merese thnku once again potd banata raho mik bhaiya
It was easy. Took me 2 mins to figure out the solution and 1 min to write the solution class Solution { public: int minChanges(string s) { int ans = 0; for(int i=0; i
It is very good that you figured it out in 2 mins and coded it in 1 min. But there are many students who are struggling. They might get demotivated by seeing these types of comment. Hope you will understand. No hate 💗 Happy coding 🙏
Pata nai ki self doubt yu karta hu, ye toh I was about to leave thinking aise question kabhi kara nai. However thode se push ke baad solved it easily with optimal approach. Bas darr reh raha hai DSA karte wqt
Sir in approach 1 when our count is odd then we have to changed the character and reinitialise ch=[i] but this time our i pointing to changed character, i think ch =s[i+1]
Rather than going to the approaches, it would be great to say why this greedy approach is a right solution and always guarantees the minimum number of changes.
bhaiya agar example case 11000111 hai to isme different different partition me no of changes different ho sakte hai to partition se to fark padega hi na ?? 11 00 01 11 change =1 , 1100 0111 change =3 hai aise kon sa partition lena hai jisme no of changes minimum ho to different possible partition vala logic se confusion hota hai please reply !!!!!
Leetcode ka itna hype bana dia hai ki question dekhte hi darr lagta hai. Lekin jab aapka explaination dekhta hoon toh lagta hain, kya hi easy question tha. Ye bhi nhi kar paaya. Zyada hi confusing language rehti hai leetcode ke questions ke... Aap kuch bata sakte ho, jisse khud solve karne mei asaani mille leetcode ke questions mei and darr ki jagah confidence aaye, ki mei bhi kar sakta hoon? Kya aapko bhi starting mei itna time lagta tha? [currently in 4th year] btw Thanks for making the explaination, so simple.
bhaiya mere dimag mein pehle edge cases jese ki smallest string length 2 ka hi ho sakta hai, to usko check kiya, aur return kar diya, and then check before partitioning, ki given string contains only one or zero which one, and return , and if one and zero dono contain kar rahe hai, tab mene string ko divide by 2 karke length mein substring mein thoda, and recursion call kiya, and after partitoning if it's odd length, then minimum count of zero and one, else if it's length 2, then check and return. but my approach passes 307/544 cases, that's why don't solved by own, solved after seeing video. Can you pls make video how to approach every problem on evry topic, it's very helpful. please
my solution: class Solution { public: int minChanges(string s) { int ans = 0; for (int i = 1; i < s.size(); i += 2) { int one = 0, zero = 0; if (s[i] == '1') { one++; } else zero++; if (s[i - 1] == '1') { one++; } else { zero++; } ans += min(one, zero); } return ans; } };
Again sorry for late submission THODA GREEDY SOCHA AUR HAR CHAGE KO CALCULATE KIYE int minChanges(string s) { int n=s.length(); int i=0; int reqBeautyChange=0; while(i O(N) SPACE => Constant O(1)
this question is not easy at all... I don't understand where I am going wrong... I have been solving questions for like one and half year now and solved more than 1000+ questions and still couldn't be able to figure out how to solve this one.....
Background me cooker ki awaaz == MIK is back to 🇮🇳 ❤
Thank you all for supporting me and understanding my delays past week 🙏♥️
= -> ❌ == ->✔
me abhi graph concepts and questions ki playlist kar rha hu, maja aa rha hai graph padhne me 😊
yayyyy
@@varunpalsingh3822 epic playlist hai bhai
Bhai.... Radhe Radhe ❤
best teacher ever
Means a lot ♥️🙏
That motivation part is back
Thanks a lot for the support sir ❤️
Using binary operation xor makes its highly efficient:
public int minChanges(String s) {
int ans = 0, i = 0;
while (i < s.length()) {
ans += s.charAt(i++) ^ s.charAt(i++); // gives 1 when last digit is set and unset in any one position
}
return ans;
}
Too good 🙌
Welcome back to 🇮🇳
Thank you so much bhai! Today I exactly copied your style and pattern to code by pausing and matched each and everyline with it!! I'm getting confident with each day because of you!
Solve it my own. Thanks for the video. Your videos really helped me to build logic.
Good Logic 👍🏻
It was literally easiest problem ever,
class Solution {
public:
int minChanges(string s) {
int n = s.length();
int count = 0;
for(int i = 0; i
Thanks MIK for the explanation. The finest explanation of all.
Here is the code
class Solution {
public:
int minChanges(string s) {
int i = 0, j = 1;
int change = 0;
while (j < s.size()) {
if (s[i] != s[j]) {
change++;
}
i += 2, j += 2;
}
return change;
}
};
keep shining as always.
int minChanges(string s) {
int n=s.length();
int ans=0;
for(int i=0;i
Awesome 🙌
hello mik bhaiya thnku so much second approach mere dimag me aya tha but sure nhi tha me toh and apka potd wala video toh dekhna hi hai if bhale hi merese problem hojaay and and 1st approach se solve kiya apke intution is next level kaffi thought process improve hui hai and second se toh hua hi merese
thnku once again potd banata raho mik bhaiya
It was easy. Took me 2 mins to figure out the solution and 1 min to write the solution
class Solution {
public:
int minChanges(string s) {
int ans = 0;
for(int i=0; i
It is very good that you figured it out in 2 mins and coded it in 1 min. But there are many students who are struggling. They might get demotivated by seeing these types of comment. Hope you will understand. No hate 💗 Happy coding 🙏
It was easy, solved by own Thank you bhaiyya
you are amazinggg
Thanks MIK , your detailed explanation is awesome . Keep going 😇
Thanks a ton ♥️🙏
Thanks a lot bhaiya ❤❤
Motivating Line💥
Pata nai ki self doubt yu karta hu, ye toh I was about to leave thinking aise question kabhi kara nai. However thode se push ke baad solved it easily with optimal approach. Bas darr reh raha hai DSA karte wqt
Sir in approach 1 when our count is odd then we have to changed the character and reinitialise ch=[i] but this time our i pointing to changed character, i think ch =s[i+1]
Thanks 😊
I want to switch to product based can you tell me what should I do apart from DSA. Currently having 1 YOE. Please guide.
Rather than going to the approaches, it would be great to say why this greedy approach is a right solution and always guarantees the minimum number of changes.
Appreciate your feedback.
Will take care of this thing ❤️
Yeh sb sawal bahut hard lagta h yr
def minChanges(self, s: str) -> int:
result = 0
for right in range(1,len(s),2):
if s[right]!=s[right-1]:
result +=1
return result
bhaiya agar example case 11000111 hai to isme different different partition me no of changes different ho sakte hai to partition se to fark padega hi na ?? 11 00 01 11 change =1 , 1100 0111 change =3 hai aise kon sa partition lena hai jisme no of changes minimum ho to different possible partition vala logic se confusion hota hai please reply !!!!!
Cooker ki siti sunai dene lag gayi means Sir ji wapis India aa gye .. :D
Just check if 2nd position character is different from 1st and 4th is different from 3rd and so on. Count only if there are different
maine 2nd approach brute force mai socha tha
int minChanges(string s) {
int n = s.length();
int cnt = 0;
int i=0;
int j=1;
while(i
Great but the for loops are more faster than while loops
Hm isko 2nd method s bnae .... yeah
Thank you...
Leetcode ka itna hype bana dia hai ki question dekhte hi darr lagta hai. Lekin jab aapka explaination dekhta hoon toh lagta hain, kya hi easy question tha. Ye bhi nhi kar paaya.
Zyada hi confusing language rehti hai leetcode ke questions ke...
Aap kuch bata sakte ho, jisse khud solve karne mei asaani mille leetcode ke questions mei and darr ki jagah confidence aaye, ki mei bhi kar sakta hoon?
Kya aapko bhi starting mei itna time lagta tha? [currently in 4th year]
btw Thanks for making the explaination, so simple.
Hi, can you please make a video about how to approach and solve problems during interview.
Practice Practice Practice!! Nothing else.
cooker ki avaz vala very funny....
my solution :
class Solution {
public:
int minChanges(string s) {
int count = 0; char prev;
for(int i = 0 ; i
Clean and crisp 👌👌👌
bhaiya mere dimag mein pehle edge cases jese ki smallest string length 2 ka hi ho sakta hai, to usko check kiya, aur return kar diya, and then check before partitioning, ki given string contains only one or zero which one, and return , and if one and zero dono contain kar rahe hai, tab mene string ko divide by 2 karke length mein substring mein thoda, and recursion call kiya, and after partitoning if it's odd length, then minimum count of zero and one, else if it's length 2, then check and return.
but my approach passes 307/544 cases, that's why don't solved by own, solved after seeing video. Can you pls make video how to approach every problem on evry topic, it's very helpful. please
Bro indirectly proves that he is a hacker 17:47
bhaiya JEE advanced 3 attempts ho gaya 🥲
class Solution {
public:
int minChanges(string s) {
int n = s.length();
char prev;
int count =0;
for(int i=0;i
Clean code 👌♥️
cooker op
my solution:
class Solution {
public:
int minChanges(string s) {
int ans = 0;
for (int i = 1; i < s.size(); i += 2) {
int one = 0, zero = 0;
if (s[i] == '1') {
one++;
} else
zero++;
if (s[i - 1] == '1') {
one++;
} else {
zero++;
}
ans += min(one, zero);
}
return ans;
}
};
Clean 👌
Again sorry for late submission
THODA GREEDY SOCHA AUR HAR CHAGE KO CALCULATE KIYE
int minChanges(string s) {
int n=s.length();
int i=0; int reqBeautyChange=0;
while(i O(N)
SPACE => Constant O(1)
this question is not easy at all... I don't understand where I am going wrong... I have been solving questions for like one and half year now and solved more than 1000+ questions and still couldn't be able to figure out how to solve this one.....
😂
Cooker background noise is an emotion 😉
thank you bhaiya, for this video❤