Count Common Words With One Occurrence

🏠 ⬅️ ➡️

Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays.

Example 1:

Input: words1 = [ "leetcode ", "is ", "amazing ", "as ", "is "], words2 = [ "amazing ", "leetcode ", "is "] Output: 2 Explanation: - "leetcode " appears exactly once in each of the two arrays. We count this string. - "amazing " appears exactly once in each of the two arrays. We count this string. - "is " appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string. - "as " appears once in words1, but does not appear in words2. We do not count this string. Thus, there are 2 strings that appear exactly once in each of the two arrays.

Example 2:

Input: words1 = [ "b ", "bb ", "bbb "], words2 = [ "a ", "aa ", "aaa "] Output: 0 Explanation: There are no strings that appear in each of the two arrays.

Example 3:

Input: words1 = [ "a ", "ab "], words2 = [ "a ", "a ", "a ", "ab "] Output: 1 Explanation: The only string that appears exactly once in each of the two arrays is "ab ".

Constraints:

  • 1 <= words1.length, words2.length <= 1000
  • 1 <= words1[i].length, words2[j].length <= 30
  • words1[i] and words2[j] consists only of lowercase English letters.

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

    integer :: i, j, n, m
    character(len=30), allocatable :: words1(:)
    character(len=30), allocatable :: words2(:)
    integer, allocatable :: counts(:)

    call get_command_argument(1, value=n)
    allocate(words1(n))
    call get_command_argument(2, value=m)
    allocate(words2(m))

    do i = 1, n
        call get_command_argument(i+2, value=words1(i))
    end do

    do j = 1, m
        call get_command_argument(j+n+2, value=words2(j))
    end do

    allocate(counts(size(words1)))

    counts = 0

    do i = 1, size(words1)
        do j = 1, size(words2)
            if (words1(i) == words2(j)) then
                counts(i) = counts(i) + 1
            end if
        end do
    end do

    print '(I0)', sum(counts == 1)

end program main
Compiled
Executed
Correct
program count_common_words
      implicit none

      integer :: i, j, n, m, count
      character(len=30) :: words1(1000), words2(1000)

      ! Read the number of words in each array
      read(*,*) n
      read(*,*) m

      ! Read the words in each array
      do i = 1, n
         read(*,*) words1(i)
      end do

      do j = 1, m
         read(*,*) words2(j)
      end do

      ! Initialize the count to 0
      count = 0

      ! Check if each word in words1 appears in words2 exactly once
      do i = 1, n
         do j = 1, m
            if (words1(i) == words2(j)) then
               count = count + 1
               exit
            end if
         end do
      end do

      ! Print the final count
      write(*,*) count

      end program count_common_words
🌐 Data from online sources
def rearrangeArray(nums):
    nums.sort()
    for i in range(1, len(nums) - 1, 2):
        nums[i], nums[i + 1] = nums[i + 1], nums[i]
    return nums

The algorithm first sorts the array. Then, we swap every two adjacent elements starting from the first index until the end of the array. By doing this, we can ensure that no element will be equal to the average of its neighbors, as the elements are distinct integers and the property described in the problem cannot hold after swapping.

For example, let's take the following array:

3 1 5 9 7

Sort the array:

1 3 5 7 9

Swap every two adjacent elements starting from the first index:

1 5 3 9 7

Now we can observe that no element in the array is equal to the average of its neighbors.

🌐 Data from online sources
#include <vector>
#include <algorithm>

std::vector<int> rearrangeArray(std::vector<int>& nums) {
    std::sort(nums.begin(), nums.end());
    for (int i = 1; i < nums.size() - 1; i += 2) {
        std::swap(nums[i], nums[i + 1]);
    }
    return nums;
}

The algorithm first sorts the array. Then, we swap every two adjacent elements starting from the first index until the end of the array. By doing this, we can ensure that no element will be equal to the average of its neighbors, as the elements are distinct integers and the property described in the problem cannot hold after swapping.

For example, let's take the following array:

3 1 5 9 7

Sort the array:

1 3 5 7 9

Swap every two adjacent elements starting from the first index:

1 5 3 9 7

Now we can observe that no element in the array is equal to the average of its neighbors.