Check if One String Swap Can Make Strings Equal | 2 Approaches | Leetcode 1790 | codestorywithMIK

preview_player
Показать описание

Hi Everyone, this is the 68th video of our Playlist "Leetcode Easy".
Now we will be solving an easy practice problem on Array - Check if One String Swap Can Make Strings Equal | 2 Approaches | Leetcode 1790 | codestorywithMIK
We will solve it using 2 Approaches and will see different examples to get more clarity.

Problem Name : Check if One String Swap Can Make Strings Equal | 2 Approaches | Leetcode 1790 | codestorywithMIK
Company Tags : will update later

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

Video Summary :
Approach-1 (using map and counting)
Intuition: Count the frequency of each character in both strings and track the differing positions.
If there are more than two differences or mismatched character counts, the strings can't be made equal with one swap.

Approach-2 (using counting only, no map required)
Intuition: Identify the indices where characters differ.
If exactly two positions differ, swapping these characters should make the strings equal.

✨ Timelines ✨
00:00 - Introduction
00:20 - Motivation
00:44 - Problem Explanation
02:26 - Approach-1 (Brute Force)
10:38 - Approach-2 (Optimal)
13:10 - Coding it up

#MIK #mik #Mik #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #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 #codestorywithmik #codestorywithmick #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

Solved by own, but aapka optimal code jyada clean tha Thank you bhaiiya

salmaniproductions
Автор

Bhai saab consistency at it's peak. Even when travelling you are uploading. Hats off, inspired!

aryansinha
Автор

I have one realisation being consistent daily,
When you have true purpose and work hard with honesty, everything else starts falling into place ... just keep working hard

GrowAndFlow
Автор

8:11 yahi pr mera code fata tha thnks bahut hi detail video ke liya

jain
Автор

Thanks bhaiya for your awesome explanation videos.

mohammadaftabansari
Автор

i already solved. easy tha . thanks sir

team-fanfeet
Автор

I tried this question my own passed 128 cases figured out what I have to was struggling on implementation now I got my mind clear thank you mik

rahulharsh
Автор

It's okay no problem for the delay... thank you for every day's feed <3

sahiraali
Автор

Yeh problem + 3 similar problem solve krdi❤ thanks to you bhaiya

Babysitter-dy
Автор

At this point, I'm here for the motivation!

sauravchandra
Автор

class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
int idx1 = 0;
int idx2 = 0;
bool f = false;
for(int i=0;i<s1.size();i++){
if(s1[i] != s2[i] && f==false){
idx1 = i;
f = true;
}else if(s1[i] != s2[i] && f==true){
idx2 = i;
break;
}
}
swap(s2[idx1], s2[idx2]);
return s1==s2;
}
};

I think this approach is most optimal.🙂🙃

sayakjana
Автор

1st view 1st comment & 1st like from Mandai Kala

Imrockonn
Автор

class Solution {
public boolean areAlmostEqual(String s1, String s2) {
int n = s1.length();
if(s1.equals(s2)){
return true;
}

return false;
}
int pahla = -2;
int dusra = -2;
int count = 0;
for(int i=0; i<n; i++){

count++;
if(count>2){
return false;
}
if(pahla==-2){
pahla = i;
}
else{
dusra = i;
}
}
}
if(count==2 && &&
return true;
}
return false;
}
}

Anurag-ogvd
Автор

bro please make a video about the problem "Design twitter", giving me some tough times

Afif-dn
Автор

class Solution {
public:
bool areAlmostEqual(string s1, string s2) {

vector<int>arr(26, 0);
for(int i = 0;i<s1.size();i++)
{
arr[s1[i]-'a']++;
arr[s2[i]-'a']--;
}

for(int i = 0;i<arr.size();i++)
{
if(arr[i]!= 0) return false;
}

int countPostions = 0;
for(int i = 0;i<s1.size();i++)
{
if(s1[i]!= s2[i]) countPostions++;
if(countPostions>2) return false;
}
if(countPostions==0 || countPostions ==2) return true;
return false;
}
};
// T.C : O(N)
// S.C : O(26) ~= O(1)

rohitvermamnvtechcarvons
Автор

Plz explain the question. " good days to rob the bank". Waiting for your reply

kottapallivamsi
Автор

brute force simple=
class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
unordered_map<char, int>mp1;
unordered_map<char, int>mp2;
for(auto& c:s1)
{
mp1[c]++;
}
for(auto&c:s2)
{
mp2[c]++;
}
for(auto&c:s1)
{
if(mp1[c]!=mp2[c])
{
return false;
}
}
int temp=0;
for(int i=0;i<s1.length();i++)
{
if(s1[i]!=s2[i])
{
temp++;
}
}
if(temp>2)
{
return false;
}
return true;

}
};

prathameshjadhav
Автор

I do online BCA & there are very few opportunities in my university. so,
Mik sir placement ke liye aap se guidence ki taur pe baat kr skte kya?

rickdutta
Автор

This is what i came up with:

bool areAlmostEqual(string s1, string s2) {
if(s1.length() != s2.length())
return false;

int count=0;
char first;
char second;
bool stop=false;
for(int i=0;i<s1.length();i++){
if(s1[i] != s2[i]){
count++;
if(count == 1){
first = s1[i];
second = s2[i];
}
}
if(count == 2 && s2[i] == first && s1[i] == second )
{ stop= true;
continue;
}
}
return count==0 || count ==2 && stop;
}

rajnisharma
Автор

sir, aap ek ek elemet ko kyo compare karaye hai vector ka Directly kyo nhi compare karaye hai? i am talking about freq

sandeepvishwakarma
visit shbcf.ru