Most Common Word

🏠 ⬅️ ➡️

Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

The words in paragraph are case-insensitive and the answer should be returned in lowercase.

Example 1:

Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit. ", banned = [ "hit "] Output: "ball " Explanation: "hit " occurs 3 times, but it is a banned word. "ball " occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball, "), and that "hit " isn't the answer even though it occurs more because it is banned.

Example 2:

Input: paragraph = "a. ", banned = [] Output: "a "

Constraints:

  • 1 <= paragraph.length <= 1000
  • paragraph consists of English letters, space ' ', or one of the symbols: "!?',;. ".
  • 0 <= banned.length <= 100
  • 1 <= banned[i].length <= 10
  • banned[i] consists of only lowercase English letters.

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

    character(len=:), allocatable :: paragraph
    character(len=:), allocatable :: banned(:)
    integer :: i, j, count, max_count
    character(len=:), allocatable :: most_frequent_word

    ! Examples
    paragraph = "Bob hit a ball, the hit BALL flew far after it was hit. "
    banned = ["hit"]
    call solve(paragraph, banned, most_frequent_word)
    print *, most_frequent_word

    paragraph = "a. "
    banned = []
    call solve(paragraph, banned, most_frequent_word)
    print *, most_frequent_word

contains

    subroutine solve(paragraph, banned, most_frequent_word)
        implicit none
        character(len=*), intent(in) :: paragraph
        character(len=*), intent(in) :: banned(:)
        character(len=:), allocatable, intent(out) :: most_frequent_word
        logical :: is_banned
        integer :: i, j, count, max_count

        ! Initialize variables
        max_count = 0
        most_frequent_word = ""

        ! Loop through each word in the paragraph
        do i = 1, len_trim(paragraph)
            ! Check if current word is banned
            is_banned = .false.
            do j = 1, size(banned)
                if (trim(adjustl(banned(j))) == trim(adjustl(paragraph(i:)))) then
                    is_banned = .true.
                    exit
                end if
            end do

            ! If current word is not banned, increment its frequency
            if (.not. is_banned) then
                count = count + 1
            end if

            ! Update maximum frequency
            if (count > max_count) then
                max_count = count
                most_frequent_word = trim(adjustl(paragraph(i:)))
            end if
        end do

        ! Convert most_frequent_word to lowercase
        most_frequent_word = trim(adjustl(most_frequent_word)) // " "
        most_frequent_word = tolower(most_frequent_word)

    end subroutine solve

end program main
Compiled
Executed
Correct
program most_frequent_word
      implicit none

      integer :: i, j, k, l
      character(len=1000) :: paragraph
      character(len=1000), dimension(:) :: banned
      character(len=1000), dimension(:) :: words
      integer, dimension(:), allocatable :: counts
      integer :: max_count
      character(len=1000) :: most_frequent_word

      ! Read input
      read (*, '(A)') paragraph
      read (*, *) banned

      ! Initialize variables
      words = ''
      counts = 0
      max_count = 0
      most_frequent_word = ''

      ! Tokenize paragraph into words
      do i = 1, len_trim(paragraph)
          if (paragraph(i:i) .eq. ' ') then
              words(l) = ''
              l = l + 1
          else
              words(l) = words(l) // paragraph(i:i)
          end if
      end do

      ! Count occurrences of words
      do i = 1, l
          do j = 1, size(banned)
              if (words(i) .eq. banned(j)) then
                  counts(i) = 0
                  exit
              end if
          end do
          if (counts(i) .ne. 0) then
              counts(i) = counts(i) + 1
              if (counts(i) .gt. max_count) then
                  max_count = counts(i)
                  most_frequent_word = words(i)
              end if
          end if
      end do

      ! Print output
      write (*, '(A)') most_frequent_word

      end program most_frequent_word
🌐 Data from online sources
def minSwaps(nums1, nums2):
    n = len(nums1)
    noSwap = [float('inf')] * n
    doSwap = [float('inf')] * n
    noSwap[0] = 0
    doSwap[0] = 1

    for i in range(1, n):
        if nums1[i] > nums1[i - 1] and nums2[i] > nums2[i - 1]:
            noSwap[i] = noSwap[i - 1]
            doSwap[i] = doSwap[i - 1] + 1
        if nums1[i] > nums2[i - 1] and nums2[i] > nums1[i - 1]:
            noSwap[i] = min(noSwap[i], doSwap[i - 1])
            doSwap[i] = min(doSwap[i], noSwap[i - 1] + 1)

    return min(noSwap[n - 1], doSwap[n - 1])

The algorithm uses dynamic programming. We maintain two arrays noSwap and doSwap to track the minimum number of swaps needed at each index without swapping and with swapping nums1[i] and nums2[i], respectively. The base cases are noSwap[0] = 0 and doSwap[0] = 1.

For each index i, we check the following conditions: 1. If nums1[i] > nums1[i - 1] and nums2[i] > nums2[i - 1], we don't need to swap at index i. So, we update noSwap[i] and doSwap[i] accordingly. 2. If nums1[i] > nums2[i - 1] and nums2[i] > nums1[i - 1], we need to swap either at index i or index i - 1. We update noSwap[i] and doSwap[i] with the minimum possible value.

Finally, we return the minimum of noSwap[n - 1] and doSwap[n - 1].

🌐 Data from online sources
int minSwaps(vector<int>& nums1, vector<int>& nums2) {
    int n = nums1.size();
    vector<int> noSwap(n, INT_MAX);
    vector<int> doSwap(n, INT_MAX);
    noSwap[0] = 0;
    doSwap[0] = 1;

    for (int i = 1; i < n; i++) {
        if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {
            noSwap[i] = noSwap[i - 1];
            doSwap[i] = doSwap[i - 1] + 1;
        }
        if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
            noSwap[i] = min(noSwap[i], doSwap[i - 1]);
            doSwap[i] = min(doSwap[i], noSwap[i - 1] + 1);
        }
    }

    return min(noSwap[n - 1], doSwap[n - 1]);
}

The algorithm uses dynamic programming. We maintain two arrays noSwap and doSwap to track the minimum number of swaps needed at each index without swapping and with swapping nums1[i] and nums2[i], respectively. The base cases are noSwap[0] = 0 and doSwap[0] = 1.

For each index i, we check the following conditions: 1. If nums1[i] > nums1[i - 1] and nums2[i] > nums2[i - 1], we don't need to swap at index i. So, we update noSwap[i] and doSwap[i] accordingly. 2. If nums1[i] > nums2[i - 1] and nums2[i] > nums1[i - 1], we need to swap either at index i or index i - 1. We update noSwap[i] and doSwap[i] with the minimum possible value.

Finally, we return the minimum of noSwap[n - 1] and doSwap[n - 1].