finally!! i found the great explaination from great teacher. thank you sir. And this is my frist problem from your string playlist. hope i will cover the whole 39 problems from this playlist. thanks again sir😍😍🙏
Got curious after seeing your reel in Instragram someone thanking you for your amazing explanation. You are doing Great Job , so beautifully , crystal clear explanation. Thank you ,❤
def compress(self, chars: List[str]) -> int: temp=chars[0] count=1 ans=[] start=0 for i in range(1,len(chars)): if temp==chars[i]: count+=1 elif temp!=chars[i]: ans.append(temp+str(count)) chars[start]=temp start+=1 if count>1: t=str(count) for k in range(len(t)): chars[start]=t[k] start+=1 count=1 temp=chars[i] chars[start]=temp start+=1 if count>1: t=str(count) for k in range(len(t)): chars[start]=t[k] start+=1 return start Done✅
can we use hashmap for this problem sir .. i have solved it using hashmap and the code is like this import java.util.HashMap; import java.util.Map; public class first { public static void main(String[] args) { String str = "aabbcc"; char arr[] = str.toCharArray(); HashMap hm = new HashMap(); for(int i=0;i
Hey, Can you help me understand why is this code wrong? I can't seem to figure it out. 😥 class Solution: def compress(self, nums: List[str]) -> int: mydict = dict() res = "" for i in nums: if i in mydict.keys(): mydict[i] += 1 else: mydict[i] = 1 nums = [] for key, value in mydict.items(): nums.append(str(key)) if value > 1: nums.append(str(value)) print(nums) return len(nums) PS - I suggest running this code in LeetCode UI for better understanding and also note that I have updated the input list name from 'chars' to 'nums' in the above code.
Because ‘a’ comes only once and it’s not required to write 1. If a letter comes more than once then only we write number after it as per the Qn. You can see example 2 also.
You have more time so don't worry just solve Leetcode problems in a topic wise manner like just pick one sde sheet and take some time and practice more. Everything is gona worth it 🎉
Sir can u please review my code and tell me why am I getting heap buffer overflow error , but 1 example testcase is passing int compress(vector& chars) { int n = chars.size(); int ctr = 1; char temp = chars[0]; int next_pos = 1; int flag = 0;
int i; for (i=1 ; i= n){ flag = 1; break; } if (temp != chars[next_pos]){ continue; } if (chars[i] == temp){ ctr++; } else { temp = chars[i]; chars[next_pos] = ctr + '0'; ctr = 1; int l = next_pos + 1; int r = i-1; int len = r-l+1; chars.erase(chars.begin()+l, chars.begin()+l+len); next_pos = i+1; } } chars[next_pos] = ctr + '0'; chars.erase(chars.begin()+next_pos+1, chars.end());
i do not understand this language (hindi?) but the illustrations were enough for me to follow through!. thank you! (dhanyavad?)
Most Welcome Adom ❤️
@@codestorywithMIK same problem can you try in english
Found your channel randomly and ur explanations are amazing!! I hope you reach great levels 👊🏻
Thanks a lot ♥️♥️♥️
@@codestorywithMIK why curr_char is not compared with chars[i+1] ??? please ans
This is so beautifully explained. Loved your way. Thank you so much for this!!
loved how calm and composed you were during the explanation
finally!! i found the great explaination from great teacher. thank you sir. And this is my frist problem from your string playlist. hope i will cover the whole 39 problems from this playlist. thanks again sir😍😍🙏
This is so beautifully explained. Loved your way. Thank you so much for this.
Crystal clear explanation bro. Thanks a lot
nice videos broo.. too good
Thanks a lot ❤️
Nicely explained bro...🎉❤.
Welcome back bhai!!! Below is the Java implementation:
class Solution {
public int compress(char[] chars) {
int n = chars.length;
int index = 0;
int i =0;
while(i < n) {
char curr_char = chars[i];
int count = 0;
while(i < n && chars[i] == curr_char) {
i++;
count++;
}
chars[index++] = curr_char;
if(count > 1) {
String count_str = String.valueOf(count);
for(char ch : count_str.toCharArray()) {
chars[index] = ch;
index++;
}
}
}
return index;
}
}
Time Complexity??
O(n)
@@kratikharchandani7418
@@kratikharchandani7418 O(n)
Amazon explanation bhaiya 🥰
As usual ,bhaiya rocked
It means a lot. Thank you 🙏😇
Indeed! Great Explaination
Great explanation.
amazing dude
Perfect explanation
samjhate aap bhut accha ho par is video me aur confusion ho gayi
Hi Vartika, I really appreciate your feedback. Can you please mention where the confusion came. I will try my best to fix that next time
Thanks again
super approach
😇❤️🙏
Thank you sir ❤
Thanks a lot🌹
do you think the code can be optimized by using ordered map and then just storing it in the array?
I am trying this one of my own first , then I will post my solution then I will see your solution
thanks
Got curious after seeing your reel in Instragram someone thanking you for your amazing explanation. You are doing Great Job , so beautifully , crystal clear explanation. Thank you ,❤
Means a lot. Thank you ❤️😇🙏
Hello bhaiya!!
bhaiya,yeh question me thodi dikkit a rhi string conversion me toh isko bta dijiyega .
3280. Convert Date to Binary
What is the space complexity of this code?
Bhaiya approach to thik but mai java me code run karta hu .
acc to your concept if i run then it print wrong .
How to write the c and python program by using this approach?
Actually I am facing some difficulties
def compress(self, chars: List[str]) -> int:
temp=chars[0]
count=1
ans=[]
start=0
for i in range(1,len(chars)):
if temp==chars[i]:
count+=1
elif temp!=chars[i]:
ans.append(temp+str(count))
chars[start]=temp
start+=1
if count>1:
t=str(count)
for k in range(len(t)):
chars[start]=t[k]
start+=1
count=1
temp=chars[i]
chars[start]=temp
start+=1
if count>1:
t=str(count)
for k in range(len(t)):
chars[start]=t[k]
start+=1
return start
Done✅
sir please tell if i want to print the string then how to print
gfg problem
string encode(string src)
{
//Your code here
int n=src.size();
int indx=0;
string ans;
int i=0;
while(i
bool isDigit(string ch) {
if(ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5"
|| ch == "6" || ch == "7" || ch == "8" || ch == "9")
return true;
return false;
}
string decodeString(string s) {
stack st;
for(char ch:s) {
if(ch != ']') {
st.push(string(1, ch));
continue;
}
string temp = "";
while(!st.empty() && st.top() != "[") {
temp = st.top() + temp;
st.pop();
}
if(!st.empty()) {
st.pop();
}
string num = "";
while(!st.empty() && isDigit(st.top())) {
num = st.top() + num;
st.pop();
}
string add = "";
int times = stoi(num);
while(times--) {
add += temp;
}
st.push(add);
}
string result = "";
while(!st.empty()) {
result = st.top() + result;
st.pop();
}
return result;
}
we are using nested while loops so the time complexity is O(n^2) right ?
can we use hashmap for this problem sir .. i have solved it using hashmap and the code is like this
import java.util.HashMap;
import java.util.Map;
public class first {
public static void main(String[] args) {
String str = "aabbcc";
char arr[] = str.toCharArray();
HashMap hm = new HashMap();
for(int i=0;i
please came up with " Remove Outermost Parentheses" solution
got to know you from instagram
amazing explaination
keep stepping up your marketing game !!
Me too😂
Problem explanation was fabulous but too much while loop to solve this problem that is not optimize I think
Hey,
Can you help me understand why is this code wrong? I can't seem to figure it out. 😥
class Solution:
def compress(self, nums: List[str]) -> int:
mydict = dict()
res = ""
for i in nums:
if i in mydict.keys():
mydict[i] += 1
else:
mydict[i] = 1
nums = []
for key, value in mydict.items():
nums.append(str(key))
if value > 1:
nums.append(str(value))
print(nums)
return len(nums)
PS - I suggest running this code in LeetCode UI for better understanding and also note that I have updated the input list name from 'chars' to 'nums' in the above code.
Print and return in same code.
Will your solution work out, if we had an array of all single characters like [a,b,c,d] ?? As we will end up over writing never seen characters
It will overwrite the same index characters with the same character
The output will be {a, b, c, d}. This solution worked for this case as well.
what will be the time complexity?
It’s linear only. We are iterating over the input array of length n. Each letter visited only once.
in example 3 why its ab12 instead of a1b12
Because ‘a’ comes only once and it’s not required to write 1.
If a letter comes more than once then only we write number after it as per the Qn. You can see example 2 also.
Can anyone tell me how to think this type of approach ..Currently I'm in my 2nd year but still can't able to think this type of approach.
You have more time so don't worry just solve Leetcode problems in a topic wise manner like just pick one sde sheet and take some time and practice more. Everything is gona worth it 🎉
Sir can u please review my code and tell me why am I getting heap buffer overflow error , but 1 example testcase is passing
int compress(vector& chars) {
int n = chars.size();
int ctr = 1;
char temp = chars[0];
int next_pos = 1;
int flag = 0;
int i;
for (i=1 ; i= n){
flag = 1;
break;
}
if (temp != chars[next_pos]){
continue;
}
if (chars[i] == temp){
ctr++;
}
else {
temp = chars[i];
chars[next_pos] = ctr + '0';
ctr = 1;
int l = next_pos + 1;
int r = i-1;
int len = r-l+1;
chars.erase(chars.begin()+l, chars.begin()+l+len);
next_pos = i+1;
}
}
chars[next_pos] = ctr + '0';
chars.erase(chars.begin()+next_pos+1, chars.end());
for (auto x : chars){
cout
The heap buffer overflow error in your code is likely due to the use of the chars.erase function inside the loop.
Run This :
int compress(vector& chars) {
int n = chars.size();
int ctr = 1;
char temp = chars[0];
int next_pos = 1;
int flag = 0;
for (int i = 1; i < n; i++) {
if (next_pos >= n) {
flag = 1;
break;
}
if (temp != chars[next_pos]) {
if (ctr > 1) {
string ctrStr = to_string(ctr);
for (char c : ctrStr) {
chars[next_pos] = c;
next_pos++;
}
}
temp = chars[i];
next_pos++;
ctr = 1;
} else if (chars[i] == temp) {
ctr++;
}
}
if (ctr > 1) {
string ctrStr = to_string(ctr);
for (char c : ctrStr) {
chars[next_pos] = c;
next_pos++;
}
}
chars.erase(chars.begin() + next_pos, chars.end());
return (flag == 1) ? 1 : chars.size();
}
Can anyone point the mistake in below code
var compress = function(chars) {
let j=1;
for(let i=0;i