Given a list of characters and an array of strings, find the total length of all strings in the array of strings that can be composed using the given characters.
Input: string = [“mouse”, “me”, “bat”, “lion”], chars = “eusamotb”
The strings that can be formed using the characters “eusamotb” are “mouse” and “me” and “bat”.
Length of “mouse” is 5, length of “me” is 2, and length of “bat” is 3
Sum of all lengths = 5 + 2 + 3 = 10.
Input: string = [“hi”, “data”, “geeksforgeeks”], chars = “tiadha”
The strings that can be formed using the characters “tiadha” are “hi” and “data”. Where length of “hi” is 2, length of “data” is 4, the sum of all is 2 + 4 = 6.
To solve the problem mentioned above we have to follow the steps given below:
- We can use characters from the given character string that is ‘chars’ while forming a string. We can also reuse the used characters for forming the next string
- Maintain an unordered map with character as a key and the value by keeping track of the frequency of each character from the string of chars.
- Every time we scan characters from the list of string we reduce the frequency of character from the unordered map but we have to maintain the copy of the original map so as to check the second string .
- If the key is not present in the map it creates one with default value as zero rather than throwing an error.
Below is the implementation of the above approach:
Time Complexity: O(n * m), where n is the length of the char and m is the length of the string.
Auxiliary Space Complexity: O(1), as the undordered map will be of size 26 only.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Count of non-palindromic strings of length M using given N characters
- Maximum count of sub-strings of length K consisting of same characters
- Concatenated string with uncommon characters of two strings
- All possible strings of any length that can be formed from a given string
- Count of sub-strings of length n possible from the given string
- Maximum length of balanced string after swapping and removal of characters
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Length of the smallest sub-string consisting of maximum distinct characters
- Find the number of strings formed using distinct characters of a given string
- Check if a string can be split into two strings with same number of K-frequent characters
- Number of strings of length N with no palindromic sub string
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count the number of strings in an array whose distinct characters are less than equal to M
- Strings formed from given characters without any consecutive repeating characters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Weighted sum of the characters of a string in an array | Set 2
- Count of strings in the first array which are smaller than every string in the second array
- Frequency of a string in an array of strings
- Strings from an array which are not prefix of any other string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.