1160. Find Words That Can Be Formed by Characters

1160. Find Words That Can Be Formed by Characters

You are given an array of strings words and a string chars.

A string is good if it can be formed by characters from chars (each character can only be used once).

Return the sum of lengths of all good strings in words.

Example 1:

Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

Example 2:

Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.

Constraints:

  • 1 <= words.length <= 1000

  • 1 <= words[i].length, chars.length <= 100

  • words[i] and chars consist of lowercase English letters.

Explanation:

words = ["cat","bt","hat","tree"], chars = "atach"

In words array check that every character in each word present in the chars string.

Example: “cat” => check ‘c’ is present in chars = “atach”

check every character present in the chars.

case 1: If all the characters of word present in the chars then count the length of the string else not count the length.

case 2: Here each character can only be used once i.e,

Example word = “catt” => only one ‘t’ is present but word contain 2 ‘t’s chars = “atach” so we cannot count not true.

Method 1:

Step 1:

creating a charArr for counting the characters in chars string.

Here count the characters and store the count in its location.

Index 0 = ‘a’

……

……

Index 25 = ‘z’

Step 2:

Staring looping the given words array.

For every word call the check method ( check(String word, int[] charsArr) ) by passing the word and charsArr.

Step 3:

In check method

Create new array for counting the characters for every word.

Step 4:

If the check method return true then it count and store the length of the word in sum variable.

return the sum.

CODE:

public class FindWordsFormedChar {
  public static void main(String[] args) {
    String[] words = {"cat","bt","hat","tree"};
    String chars = "atach";
    System.out.println("Words length: "+countCharacters(words, chars));
  }

  public static int countCharacters(String[] words, String chars) {
    //step 1
    int[] charsArr = new int[26];
    for(int i=0;i<chars.length();i++){
      charsArr[chars.charAt(i)-'a']++;
    }
    int sum=0;
    //step 2
    for(String s:words){
      if(check(s,charsArr)){
        sum+=s.length();
      }
    }
    return sum;
  }

  public static boolean check(String s,int[] charsArr){
    int[] wordArr = new int[26];

    for(int i=0;i<s.length();i++){
      int m = s.charAt(i)-'a';
      wordArr[m]++;
      if(wordArr[m] > charsArr[m]){
        return false;
      }
    }
    return true;
  }
}

Output:

Words length: 6

Solution Link: https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/submissions/1110679993/

Bonus:

ASCII values to digits and alphabet

You can find all the 0–127 ascii values below 👇👇

https://www.w3schools.com/charsets/ref_html_ascii.asp

Thank you for reading.

Happy Coding😊…

Did you find this article valuable?

Support Sirisha Challagiri by becoming a sponsor. Any amount is appreciated!