LeetCode Find Common Characters Solution Explained - Java

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


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Brilliant solution Nick! 🎉🎉🎉

For those who didn't understand
For example,
• Assume our Input is ["abc", "cde"]
• At first min_frequencies is filled with large values because later when we do Math.min, it helps for the first loop.

• In the first inner for loop, with string, "abc"
The char_frequencies = [a = 1, b = 1, c = 1, every other char is zero]
• In the second inner for loop, we update min_frequencies[i] with min value of min_frequencies[i] and char_frequencies[i] array. After this our min_frequencies array looks like [a = 1, b = 1, c = 1, every other char is zero].

• For string "cde",
After first inner for loop, char_frequencies = [c = 1, d = 1, e = 1, every other char is zero].
• After second inner for loop, min_frequencies = [c = 1] and every other characters in min_frequencies array is zero because we take Math.min value of both arrays. "a" was 1 in old min_frequencies array but "a" is 0 in the current char_frequencies array. So in new min_frequencies "a" is 0, because we take Math.min

• So output is ["c"].

clamisland
Автор

In python its super easy:


class Solution(object):
def commonChars(self, A):
"""
:type A: List[str]
:rtype: List[str]
"""
c = collections.Counter(A[0])
for i in range(1, len(A)):
c &= collections.Counter(A[i])
return list(c.elements())

hemantjain
Автор

brilliant idea. use min_frequency to filter the common letters.

alexhong
Автор

Great thinking!! Really videos are just an example of how should anyone approach a problem!

poojaguru
Автор

This is actually hard for an easy Leetcode problem.

tranminhquang
Автор

Thank you @Clam Island.Your explanation helped

jax
Автор

Why you took the minimum value of each character ?

sanyamgoel
Автор

didn't get the 2nd last for loop i.e Math.min one

manavmalhotra
Автор

class Solution {
public List<String> commonChars(String[] A) {
List<String> list = new ArrayList<String> ();

char[] temp = A[0].toCharArray();
System.out.println(new String(temp));

for(int i=0; i<A.length-1; i++){
String cal = compareStr(temp, A[i+1].toCharArray());
temp = cal.toCharArray();
System.out.println(new String(temp));
}

for(int i=0; i<temp.length; i++){
list.add(temp[i]+"");
}

return list;
}

public String compareStr(char[] str1, char[] str2){
StringBuffer sb = new StringBuffer();

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

for(int j=0; j <str2.length; j++){
if (str2[j] == str1[i]){
sb.append(str1[i]+"");
str2[j] =' ';
break;
}
}
}

return sb.toString();

}
}

bistvijay
Автор

No way I could train my brain to come up with something like that without memorizing it.

valkon_
Автор

is there anyone who solve with HashMap?

доброговременишуток