tutorialcup
Find Common Characters Leetcode Solution
Problem Statement
In this problem, we are given a list of string. We have to find out the characters that are common in all the strings. If a character is present in all strings in multiple times, then we have to output the character multiple times.
Suppose, we have array of strings
We can see that, character 'e' is present once in all strings, l is present twice in all strings. No other character is common.
So, in our output list, character 'e' will be present once and character 'l' will be present twice.
Example
Approach
We can see that we have to find out the common frequency of characters a-z in all strings here.
For every string we can create a count array of size 26, which is having frequency of characters a-z. index 0 will have count of 'a' in that string and index 1 has count of 'b' and so on.
Now for each character from a to z, we have to find out the minimum count which may be present in any of the arrays created above. We are doing this, because we are interested in minimum presence of a character in all strings. In other words, we are only taking common characters from each string.
So, we will firstly create an ans array of size 26 which is having all indexes set at max value.
Then, we will traverse the array of strings from left to right. In each step, we will create the count array for current string. Then we will compare the current created array with ans array.
Because
www.tutorialcup.com/leetcode-solutions/find-common-charac...
Find Common Characters Leetcode Solution
Problem Statement
In this problem, we are given a list of string. We have to find out the characters that are common in all the strings. If a character is present in all strings in multiple times, then we have to output the character multiple times.
Suppose, we have array of strings
We can see that, character 'e' is present once in all strings, l is present twice in all strings. No other character is common.
So, in our output list, character 'e' will be present once and character 'l' will be present twice.
Example
Approach
We can see that we have to find out the common frequency of characters a-z in all strings here.
For every string we can create a count array of size 26, which is having frequency of characters a-z. index 0 will have count of 'a' in that string and index 1 has count of 'b' and so on.
Now for each character from a to z, we have to find out the minimum count which may be present in any of the arrays created above. We are doing this, because we are interested in minimum presence of a character in all strings. In other words, we are only taking common characters from each string.
So, we will firstly create an ans array of size 26 which is having all indexes set at max value.
Then, we will traverse the array of strings from left to right. In each step, we will create the count array for current string. Then we will compare the current created array with ans array.
Because
www.tutorialcup.com/leetcode-solutions/find-common-charac...