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.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
temp.f95:9:39: 9 | call get_command_argument(1, value=n) | 1 Error: In call to ‘get_command_argument’ at (1), type mismatch in argument ‘value’; pass ‘INTEGER(4)’ to ‘CHARACTER(*)’ temp.f95:11:39: 11 | call get_command_argument(2, value=m) | 1 Error: In call to ‘get_command_argument’ at (1), type mismatch in argument ‘value’; pass ‘INTEGER(4)’ to ‘CHARACTER(*)’ temp.f95:34:22: 34 | print '(I0)', sum(counts == 1) | 1 Error: ‘array’ argument of ‘sum’ intrinsic at (1) must have a numeric type
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
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78097bbb6960 in ??? #1 0x78097bbb74d9 in ??? #2 0x78097be0b17b in ??? #3 0x78097be04684 in ??? #4 0x78097be052aa in ??? #5 0x5b944393e22c in MAIN__ #6 0x5b944393e542 in main
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.
#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.