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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=:), allocatable :: text
    character(len=:), allocatable :: brokenLetters
    integer :: nWords

    ! Example 1
    allocate(character(len=7) :: text)
    allocate(character(len=2) :: brokenLetters)
    text = "hello world"
    brokenLetters = "ad"
    write(*,*) solve(text, brokenLetters)

    ! Example 2
    allocate(character(len=8) :: text)
    allocate(character(len=2) :: brokenLetters)
    text = "leet code"
    brokenLetters = "lt"
    write(*,*) solve(text, brokenLetters)

    ! Example 3
    allocate(character(len=9) :: text)
    allocate(character(len=1) :: brokenLetters)
    text = "leet code"
    brokenLetters = "e"
    write(*,*) solve(text, brokenLetters)

contains

    function solve(text, brokenLetters) result(nWords)
        implicit none
        character(len=*), intent(in) :: text
        character(len=*), intent(in) :: brokenLetters
        integer :: i, j, k, nWords
        logical :: found

        nWords = 0
        do i = 1, len_trim(text)
            if (text(i:i) == ' ') then
                nWords = nWords + 1
            else
                found = .false.
                do j = 1, len_trim(brokenLetters)
                    if (text(i:i) == brokenLetters(j:j)) then
                        found = .true.
                        exit
                    end if
                end do
                if (.not. found) then
                    nWords = nWords + 1
                end if
            end if
        end do
    end function solve

end program main
Compiled
Executed
Correct
program keyboard
      implicit none

      integer :: i, j, n, m
      character(len=100) :: text, brokenLetters
      character(len=100) :: word
      integer :: numWords

      ! Read input
      read(*,*) text
      read(*,*) brokenLetters

      ! Initialize variables
      n = len_trim(text)
      m = len_trim(brokenLetters)
      numWords = 0

      ! Iterate over words in text
      do i = 1, n
          if (text(i:i) == ' ') then
              ! Found a word
              word = text(j:i-1)
              j = i + 1

              ! Check if word can be typed
              if (checkWord(word, brokenLetters)) then
                  numWords = numWords + 1
              end if
          end if
      end do

      ! Print output
      write(*,*) numWords

      contains

      ! Check if word can be typed using the broken keyboard
      function checkWord(word, brokenLetters) result(canType)
          implicit none
          character(len=*), intent(in) :: word
          character(len=*), intent(in) :: brokenLetters
          integer :: i, m
          logical :: canType

          ! Initialize variables
          m = len_trim(brokenLetters)
          canType = .true.

          ! Iterate over letters in word
          do i = 1, len_trim(word)
              ! Check if letter is in brokenLetters
              if (index(brokenLetters, word(i:i)) /= 0) then
                  ! Letter is broken, cannot type word
                  canType = .false.
                  exit
              end if
          end do

          ! Return result
          return
      end function checkWord
      end program keyboard
🌐 Data from online sources
def min_operations(n):
    count = 1
    temp = 1

    while temp % n != 0:
        temp = (temp * 2) % n
        count += 1

    return count

The algorithm starts by initializing a counter count and a temporary variable temp to store the intermediate values. For each operation, the algorithm updates the value of temp by multiplying it by 2 and taking the modulo of n. This is repeated until the temporary value becomes zero modulo n (temp % n == 0), which means that the original permutation has been reached again. The counter count is updated after each iteration, and the algorithm returns the value of count as the minimum non-zero number of operations needed.

🌐 Data from online sources
int min_operations(int n) {
    int count = 1;
    int temp = 1;

    while (temp % n != 0) {
        temp = (temp * 2) % n;
        count++;
    }

    return count;
}

The algorithm starts by initializing a counter count and a temporary variable temp to store the intermediate values. For each operation, the algorithm updates the value of temp by multiplying it by 2 and taking the modulo of n. This is repeated until the temporary value becomes zero modulo n (temp % n == 0), which means that the original permutation has been reached again. The counter count is updated after each iteration, and the algorithm returns the value of count as the minimum non-zero number of operations needed.