I code in python but i guess, you'll understand ;) class Solution(object): def commonChars(self, words): """ :type words: List[str] :rtype: List[str] """ q = "abcdefghijklmnopqrstuvwxyz" res = [] for char in q: min_count = min(word.count(char) for word in words) res.extend([char] * min_count) return res
I tried to take only one array and added all counts and only added the characters into the resultant array that had a frequency in multiples of words.size(). Will this work ?
class Solution { public List commonChars(String[] words) { int[] minFreq = count(words[0]); for(int i = 1; i < words.length; i++){ minFreq = intersection(minFreq, count(words[i])); } List res = new ArrayList(); for(int i = 0; i < 26; i++) { if(minFreq[i] != 0){ char ch = (char)(i+'a'); int count = minFreq[i]; while(count > 0){ res.add(""+ ch); count--; } } } return res; } public int[] intersection(int[] a, int[] b){ int res[] = new int[26]; for(int i = 0; i < 26; i++){ res[i] = Math.min(a[i], b[i]); } return res; } public int[] count(String str){ int[] res = new int[26]; for(char c : str.toCharArray()){ int idx = c - 'a'; res[idx]++; } return res; } }
solve without using extra space class Solution { public List commonChars(String[] words) { List ans = new ArrayList(); String first = words[0]; for (int i = 0; i < words[0].length(); i++) { boolean mainFlag = true; char ch = words[0].charAt(i); System.out.println("ch is " + ch); for (int j = 1; j < words.length; j++) { boolean flag = false; for (int k = 0; k < words[j].length(); k++) { if (ch == words[j].charAt(k)) { flag = true; String modified = words[j].replaceFirst(Character.toString(words[j].charAt(k)), "*"); words[j] = modified; System.out.println(modified); break; } } if (flag == false) { mainFlag = false;
} } if (mainFlag) { ans.add(Character.toString(ch));
like target of this video is 50. If you have other solution, share in comments..
I code in python but i guess, you'll understand ;)
class Solution(object):
def commonChars(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
q = "abcdefghijklmnopqrstuvwxyz"
res = []
for char in q:
min_count = min(word.count(char) for word in words)
res.extend([char] * min_count)
return res
thanks for uploading so many insightful videos. thank you sir
class Solution {
public:
vector commonChars(vector& words) {
int n=words.size();
vector ans;
vector previous(26,INT_MAX);
vector current(26,0);
for(int i=0;i
first comment.. I was waiting.. thanks for uploading so many insightful videos. Really appreciate your work sir
Bhaiya, June start ho gya, DP start kr do please🙏🙏🙏🙏🙏 ya phir Graphs ki series.
I tried to take only one array and added all counts and only added the characters into the resultant array that had a frequency in multiples of words.size(). Will this work ?
class Solution {
public List commonChars(String[] words) {
int[] minFreq = count(words[0]);
for(int i = 1; i < words.length; i++){
minFreq = intersection(minFreq, count(words[i]));
}
List res = new ArrayList();
for(int i = 0; i < 26; i++) {
if(minFreq[i] != 0){
char ch = (char)(i+'a');
int count = minFreq[i];
while(count > 0){
res.add(""+ ch);
count--;
}
}
}
return res;
}
public int[] intersection(int[] a, int[] b){
int res[] = new int[26];
for(int i = 0; i < 26; i++){
res[i] = Math.min(a[i], b[i]);
}
return res;
}
public int[] count(String str){
int[] res = new int[26];
for(char c : str.toCharArray()){
int idx = c - 'a';
res[idx]++;
}
return res;
}
}
Neat!!!👏
@@shashwat_tiwari_st Thanks Sir
any plans for videos related to development?
solve without using extra space
class Solution {
public List commonChars(String[] words) {
List ans = new ArrayList();
String first = words[0];
for (int i = 0; i < words[0].length(); i++) {
boolean mainFlag = true;
char ch = words[0].charAt(i);
System.out.println("ch is " + ch);
for (int j = 1; j < words.length; j++) {
boolean flag = false;
for (int k = 0; k < words[j].length(); k++) {
if (ch == words[j].charAt(k)) {
flag = true;
String modified = words[j].replaceFirst(Character.toString(words[j].charAt(k)), "*");
words[j] = modified;
System.out.println(modified);
break;
}
}
if (flag == false) {
mainFlag = false;
}
}
if (mainFlag) {
ans.add(Character.toString(ch));
}
}
return ans;
}
}
Sir tree series k baad kya padhne wale hai aur kab se please reply
Bhaiya par ye sab toh online assessment toh puch ta hu nahi
Online assessment kaise face kare uska bhi koi video banao na