Find Common Characters | Simple Dry Run | Leetcode 1002 | codestorywithMIK

preview_player
Показать описание
This is the 41st Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a good practice Array+String+Map problem : Find Common Characters | Simple Dry Run | Leetcode 1002 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Find Common Characters | Simple Dry Run | Leetcode 1002 | codestorywithMIK
Company Tags : will update soon

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

Summary :
We need to find the common characters in a list of strings. Here's a summary of the approach :

fillCountArray Method: This helper method takes a string and an array of 26 integers (representing counts for each letter of the alphabet). It increments the counts for each character in the string.

commonChars Method:

Initialization: The method initializes a vector of strings (result) to store the common characters and an integer array count of size 26 to keep track of character frequencies in the first word.
Count Update: It calls fillCountArray on the first word to populate count. Then, for each subsequent word, it uses a temporary count array temp to store character frequencies.
Frequency Comparison: For each character, it updates count to the minimum frequency of that character between the current count and temp.
Result Construction: Finally, it iterates over the count array, and for each non-zero count, it adds the corresponding character to the result the number of times it appears.
Return: The method returns the vector of common characters.

In essence, the solution identifies the minimum frequency of each character across all words and constructs the list of common characters based on these frequencies.

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
Рекомендации по теме
Комментарии
Автор

i just woke and tried the pod of leetcode..but i could not able to solve..so i was waiting for your video .Again You Made my morning....thnku bhaiya

shivamsingh-mimn
Автор

OMG I solved the question on my own and my code is ditto same as your.

shibainu
Автор

guess what been trying this problem from yesterday

crazy that IT came as potd today


crazy^2 that you made a video about it

xd

nerduser
Автор

I think this could have been marked MEDIUM
Its a good one. Thanks for explaining so well

DevOpskagyaan
Автор

class Solution {
public:
vector<string> commonChars(vector<string>& words) {
vector<int> cnt(26, 0);
vector<string> ans;
for(int i = 0;i < words[0].size();i++){
cnt[words[0][i] - 'a']++;
}
for(int i = 1; i < words.size();i++){
string s = words[i];
vector<int> cnt1(26, 0);
for(char ch : s){
cnt1[ch-'a']++;
}
for(int j = 0; j < 26; j++){
cnt[j] = min(cnt[j], cnt1[j]);
}
}
for(int i = 0;i < 26;i++){
int no = cnt[i];
while(no--) ans.push_back(string(1, 'a'+i));
}
return ans;
}
};❤❤

utkarshsahay
Автор

class Solution {
public:
vector<string> commonChars(vector<string>& words) {
int n=words.size();
vector<int> freq(26, 0);
for(auto ch:words[0]) freq[ch-'a']++;
for(int i=1;i<words.size();i++)
{
vector<int> lf(26, 0);
for(char ch: words[i])
{
lf[ch-'a']++;
}
for(char ch='a';ch<='z';ch++)
{
freq[ch-'a']= min(freq[ch-'a'], lf[ch-'a']);
}
}
// for(auto it: freq){
// cout<<it<<", ";
// }
vector<string> v;
for(int i=0;i<26;i++)
{
int f=freq[i];
char ch='a'+i;
string s="";
s+=ch;
while(f--)
{
v.push_back(s);
}
}
return v;
}
};

unknown
Автор

bhaiya please segment tree sikha do pls, 1 saal se try kr raha hun sikhne ka aap hi sikha skte ho.
ek 3-4 ghnte ki video bna do, 5-6 questions krwa do along with basic code, please bhaiya

gauravkumar
Автор

congrats sir for 50k subs. i hope the count will keep going. please take a glance on gfg potd today's as well. a good prefix sum question.

kumarashutosh-pn
Автор

bhaiya gfg ka potd aur start kardo plz plz
Thanks in advance❤❤

AdityaGupta-swzr
Автор

please bro explain :- 474. Ones and Zeroes question

PANKAJKUMAR-nxkv
Автор

Bhaiya please longest valid parantheses q ka video laiye important q h interview k liye

pokeindia
Автор

class Solution {
public:
vector<string> commonChars(vector<string>& words) {

int n=words.size();

vector<string> ans;

for(int i=0;i<26;i++){

char ch=i+'a';
int charCount=INT_MAX;
for(auto x:words){
int c=0;
for(int j=0;j<x.size();j++){
if(ch ==x[j]){
c++;
}
}
charCount=min(charCount, c);
}

if(charCount!=INT_MAX){
for(int j=0;j<charCount;j++){
string str="";
str.push_back(ch);
ans.push_back(str);
}
}

}

return ans;
}
};

sunilsarode
Автор

I wrote this solution on my own
strangely this also passed with 4 ms only


class Solution {
public List<String> commonChars(String[] words) {
int n = words.length;
List<String> ans = new ArrayList<>();

int[][] freq = new int[n][26];

for(int i=0;i<n;i++){
String str = words[i];

for(int j=0;j<str.length();j++){
char ch = str.charAt(j);

}
}
char ch='a';

for(int i=0;i<26;i++){
int min=Integer.MAX_VALUE;
boolean hasChar=true;
for(int j=0;j<n;j++){
if(freq[j][i]==0){
hasChar=false;
}
else{
min=Math.min(min, freq[j][i]);
}
}
if(hasChar){
while(min>0){
ans.add(String.valueOf(ch));
min--;
}
}
ch++;
}

return ans;
}
}

aizadiqbal
Автор

sir please put the solution of leetcode 3122 .I am stuck with this problem

shivanshishrivastava
Автор

bhaiya apny ye konsa function use kiya in line 31

Manjot_singh
Автор

bhai or to thik hai pr more pr click krne pr github ka link or whatapp ka link nhi aa rha

nishantthakur
Автор

what is the problem with this code :




class Solution {
public:
void fillcount(string &word, int count[26])
{
for(char &c: word)
{
count[c-'a']++;

}
}
vector<string> commonChars(vector<string>& words) {
vector<string>result;
int n=words.size();
int count[26]={0};

fillcount(words[0], count);

for(int i=1;i<n;i++)
{
int temp[26]={0};
fillcount(words[i], temp);
for(int j=0;j<26;j++)
{
count[j]=min(count[j], temp[j]);

}
}


for(int i=0;i<26;i++)
{
if(count[i]!=0)
{
result.push_back(string(1, i+'a'));
}
}
return result;
}
};

Avatar
Автор

BRO PLEASE ANSWER THIS QUESTION QUESTION IS IF WE CAN FORM TARGET STRING FROM ARRYA ELEMENTS
import java.util.ArrayList;

public class Solution {
static ArrayList<ArrayList<String>> res = new ArrayList<>(); // Not used in this version

public static void main(String[] args) {
String[] arr = {"os", "mn", "pqr", "qr"};
String t = "pqros"; //THIS SHOULD RETURN TRUE BCOZ PQR+OS is forming target but it is giivng false should return true technically
String sofar = "";
ArrayList<String> l = new ArrayList<>();
System.out.println(canconst(arr, t, sofar, 0, l));
}

public static boolean canconst(String[] arr, String t, String sofar, int idx, ArrayList<String> l) {
if(sofar.equals(t))
{
return true;
}
if (idx >= arr.length) {
return false;
}
// Try appending each string from arr to sofar and recursively check
if (canconst(arr, t, sofar + arr[idx], idx+1, l)) {
return true;
}
return canconst(arr, t, sofar, idx + 1, l);
}
}
//THIS SHOULD RETURN TRUE BCOZ PQR+OS is forming target but it is giivng false should return true technically
this code is only giivng correct output if the we are checking the idx elements sequence wise why so please answer what changes should i make in my code so that it works
galti pakad nai aa rahi please help bhai do ghante ho gaye hit and trial kar raha hu doubt clear kardo aisa kyu horaha hai pLEASE DO REPLY ITS A REQUEST

nish
Автор

any other method of this question to solve???

Mohit-fehx
Автор

This is definitely not a easy question

rohitaggarwal