LeetCode 1935: Maximum Number of Words You Can Type Solution

Master LeetCode problem 1935 (Maximum Number of Words You Can Type), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1935. Maximum Number of Words You Can Type

Problem Explanation

Explanation:

To solve this problem, we need to count the number of words in the given text that can be fully typed using the functioning keys on the keyboard. We can iterate through each word in the text and check if all the letters in that word are present in the set of functioning keys. If any letter in the word is part of the broken letters set, we skip that word.

Algorithm:

  1. Create a set to store the broken letters.
  2. Split the text into words using space as a delimiter.
  3. Initialize a variable count to 0.
  4. Iterate through each word in the text.
    • For each word, iterate through its characters.
    • If any character is present in the set of broken letters, break and move to the next word.
    • If all characters are valid, increment the count.
  5. Return the count.

Time Complexity:

The time complexity of this algorithm is O(n*m), where n is the number of words in the text and m is the average length of a word.

Space Complexity:

The space complexity is O(m), where m is the number of distinct broken letters.

:

Solution Code

class Solution {
    public int canBeTypedWords(String text, String brokenLetters) {
        Set<Character> brokenSet = new HashSet<>();
        for (char c : brokenLetters.toCharArray()) {
            brokenSet.add(c);
        }
        
        String[] words = text.split(" ");
        int count = 0;
        
        for (String word : words) {
            boolean canType = true;
            for (char c : word.toCharArray()) {
                if (brokenSet.contains(c)) {
                    canType = false;
                    break;
                }
            }
            if (canType) {
                count++;
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1935 (Maximum Number of Words You Can Type)?

This page provides optimized solutions for LeetCode problem 1935 (Maximum Number of Words You Can Type) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1935 (Maximum Number of Words You Can Type)?

The time complexity for LeetCode 1935 (Maximum Number of Words You Can Type) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1935 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1935 in Java, C++, or Python.

Back to LeetCode Solutions