1935. Maximum Number of Words You Can Type LeetCode Solution

In this guide, you will get 1935. Maximum Number of Words You Can Type LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Words You Can Type problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Number of Words You Can Type solution in C++
  4. Maximum Number of Words You Can Type solution in Java
  5. Maximum Number of Words You Can Type solution in Python
  6. Additional Resources
1935. Maximum Number of Words You Can Type LeetCode Solution image

Problem Statement of Maximum Number of Words You Can Type

There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
Given a string text of words separated by a single space (no leading or trailing spaces) and a string brokenLetters of all distinct letter keys that are broken, return the number of words in text you can fully type using this keyboard.

Example 1:

Input: text = “hello world”, brokenLetters = “ad”
Output: 1
Explanation: We cannot type “world” because the ‘d’ key is broken.

Example 2:

Input: text = “leet code”, brokenLetters = “lt”
Output: 1
Explanation: We cannot type “leet” because the ‘l’ and ‘t’ keys are broken.

Example 3:

Input: text = “leet code”, brokenLetters = “e”
Output: 0
Explanation: We cannot type either word because the ‘e’ key is broken.

Constraints:

1 <= text.length <= 104
0 <= brokenLetters.length <= 26
text consists of words separated by a single space without any leading or trailing spaces.
Each word only consists of lowercase English letters.
brokenLetters consists of distinct lowercase English letters.

See also  1932. Merge BSTs to Create Single BST LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1935. Maximum Number of Words You Can Type LeetCode Solution in C++

class Solution {
 public:
  int canBeTypedWords(string text, string brokenLetters) {
    int ans = 0;
    istringstream iss(text);
    vector<bool> broken(26);

    for (const char c : brokenLetters)
      broken[c - 'a'] = true;

    for (string word; iss >> word;)
      ans += canBeTyped(word, broken);

    return ans;
  }

 private:
  bool canBeTyped(const string& word, const vector<bool>& broken) {
    for (const char c : word)
      if (broken[c - 'a'])
        return false;
    return true;
  }
};
/* code provided by PROGIEZ */

1935. Maximum Number of Words You Can Type LeetCode Solution in Java

class Solution {
  public int canBeTypedWords(String text, String brokenLetters) {
    int ans = 0;
    boolean[] broken = new boolean[26];

    for (final char c : brokenLetters.toCharArray())
      broken[c - 'a'] = true;

    for (final String word : text.split(" "))
      ans += canBeTyped(word, broken);

    return ans;
  }

  private int canBeTyped(final String word, boolean[] broken) {
    for (final char c : word.toCharArray())
      if (broken[c - 'a'])
        return 0;
    return 1;
  }
}
// code provided by PROGIEZ

1935. Maximum Number of Words You Can Type LeetCode Solution in Python

class Solution:
  def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
    ans = 0
    broken = set(brokenLetters)

    for word in text.split():
      ans += all(c not in broken for c in word)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.