Buddy Strings | GOOGLE | META | Leetcode-859 | Explanation ➕ Live Coding

preview_player
Показать описание
Hi everyone, this is the 15th video of our String playlist.
In this video we will try to solve an easy but a very good string problem “Buddy Strings”.

Share your learnings on LinkedIn, Twitter (X), Instagram, Facebook(Meta) with the hashtag #codestorywithmik & feel free to tag me.

We will write very easy and clean code with intuition building and understanding each line of code.
We will break the problem from examples.
We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Buddy Strings
Company Tags : GOOGLE, META

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

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

Congratulations for 5k...yeh toh abhi shuruwat hai ....aggeee bahat dur Jana hai 😊

sauravbiswajit
Автор

Agreed This Question needs Discussion. When I was doing finding difficulty but from your explanation it is now cup of tea.

AvinashKumar-pstw
Автор

buddy this is best approach i have ever seen

akshaymahajan
Автор

Soon 50k -> 100k -> 1M to this legendary channel

DevOpskagyaan
Автор

Congratulations for 5k🎊 bhaiya
thanks for the explanation

oqant
Автор

Java Code:-


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

false;

if(s.equals(goal)){
return checkFreq(s);
}

ArrayList<Integer> list=new ArrayList<>();

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


list.add(i);
}
}

//
if(list.size()!=2)return false;

String swapped = swap(list.get(0), list.get(1), s);
// System.out.println(swapped);

true;

return false;
}


private String swap(int i, int j, String s){

char arr[]=s.toCharArray();

char temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
StringBuilder sb=new StringBuilder();
for(int idx=0;idx<arr.length;idx++){
sb.append(arr[idx]);
}

return sb.toString();

}


private boolean checkFreq(String s){

int freq[]=new int[26];

for(char ch:s.toCharArray()){
freq[ch-'a']++;

if(freq[ch-'a']>1){
return true;
}
}
return false;
}
}

danishsaifi
Автор

congrats on the 5K and thanks for the explanation.

floatingpoint
Автор

Congrats for the 5k bhai!! Many more to go 🎈🎈
PS Youtube still deleting my Java sol!

JJ-tpdd
Автор

Congratulations on 5k
And clean and crisp explanation

FanIQQuiz
Автор

First of all congratulations for 5k Subscribers❤👏
I have done it in constant space, as it was mentioned that only 2 indices need to swap.
class Solution {
public:
bool isEqual(string &s, string &goal)
{
for(int i = 0; i < s.length(); i++)
{
if(s[i] != goal[i])
return false;
}
return true;
}

bool freq(string &s)
{
int arr[26] = {0};

for(int i=0; i<s.length(); i++)
{
arr[s[i] - 'a']++;
if(arr[s[i] - 'a'] >= 2)
return true;
}
return false;
}

bool buddyStrings(string s, string goal) {

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

int indices[2] = {0};

int count = 0;
for(int i = 0; i < s.length(); i++)
{
if(s[i] != goal[i])
{
if(count <= 1)
{
indices[count] = i;
}
else
{
return false;
}
count++;
}
}

if(count == 2)
{
swap(s[indices[0]], s[indices[1]]);
return isEqual(s, goal);
}

return isEqual(s, goal) && freq(s);

}
};

prabhkiratsingh
Автор

Congratulations on 5k subscribers. And nicely explained.

divyanshsagar
Автор

I won’t be surprised to see 1 M soon in your channel because you deserve it ❤❤

hypewaali
Автор

I always like your explanation but I noticed a small disadvantage if I found the same qns in a interview I should explain it's brute force as well as optimal to make a impression but you always go with optimal

prudhvirajmacherla
Автор

hi bro you can put ( index.size() > 2 return false; ) inside the loop otherwise it will take O(N) space complexity (worst case) instead of constant space

AnandKumar-kzls
Автор

Congrats for 5k bhaiya❤❤
this ques was kind of hard easy for me 😅

neelx
Автор

Ya ez toh thaa hee nahi ma jab issa dekha tabhi samaj gaya tha

amansingh.h
Автор

Can you also discuss contest problems?

ritikgupta.
Автор

Subscriber to badhega bas video dalte raho to scratch se hai aur rahega v

ezcoding
Автор

This problem was frustrating easy one😢

vineetkumar
Автор

I couldn’t understand the ch-‘a’++ part plz explain

prakhar
join shbcf.ru