tutorialcup
Find Words That Can Be Formed by Characters Leetcode Solution
Problem statement
In the problem " Find Words That Can Be Formed by Characters" we are given an array of strings that consists of lower case English alphabets (words) and a string that consists of a set of characters (chars).
Our task is to check for each string in the array if it can be formed using the characters of chars (we can use each character of char only once). In the end, we need to return the sum of the length of all the strings which can be formed using characters of chars string.
Example
words = , chars = "welldonehoneyr"
10
Explanation:
In this example, we can form hello and world using the characters of the chars string. So the total length of hello and world is 5+5=10.
Approach for Find Words That Can Be Formed by Characters Leetcode Solution
To solve this problem we will use a frequency array and that will store the count of characters present in the string. We will follow these steps to solve the problem:
- Create a frequency array and store the frequency of characters of the chars string.
- Now check each string of word array one by one.
- Create a copy of the frequency array.
- Now check each character of the selected string. If the frequency of a character in the frequency array is less than 1 then we can not form a selected string using the characters of the chars string else decrease the character frequency by 1.
www.tutorialcup.com/leetcode-solutions/find-words-that-ca...
Find Words That Can Be Formed by Characters Leetcode Solution
Problem statement
In the problem " Find Words That Can Be Formed by Characters" we are given an array of strings that consists of lower case English alphabets (words) and a string that consists of a set of characters (chars).
Our task is to check for each string in the array if it can be formed using the characters of chars (we can use each character of char only once). In the end, we need to return the sum of the length of all the strings which can be formed using characters of chars string.
Example
words = , chars = "welldonehoneyr"
10
Explanation:
In this example, we can form hello and world using the characters of the chars string. So the total length of hello and world is 5+5=10.
Approach for Find Words That Can Be Formed by Characters Leetcode Solution
To solve this problem we will use a frequency array and that will store the count of characters present in the string. We will follow these steps to solve the problem:
- Create a frequency array and store the frequency of characters of the chars string.
- Now check each string of word array one by one.
- Create a copy of the frequency array.
- Now check each character of the selected string. If the frequency of a character in the frequency array is less than 1 then we can not form a selected string using the characters of the chars string else decrease the character frequency by 1.
www.tutorialcup.com/leetcode-solutions/find-words-that-ca...