Given two string arrays word1
and word2
, return true
if the two arrays represent the same string, and false
otherwise.
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input: word1 = [ "ab ", "c "], word2 = [ "a ", "bc "] Output: true Explanation: word1 represents string "ab " + "c " -> "abc " word2 represents string "a " + "bc " -> "abc " The strings are the same, so return true.
Example 2:
Input: word1 = [ "a ", "cb "], word2 = [ "ab ", "c "] Output: false
Example 3:
Input: word1 = [ "abc ", "d ", "defg "], word2 = [ "abcddefg "] Output: true
Constraints:
1 <= word1.length, word2.length <= 103
1 <= word1[i].length, word2[i].length <= 103
1 <= sum(word1[i].length), sum(word2[i].length) <= 103
word1[i]
and word2[i]
consist of lowercase letters.program main
implicit none
integer :: i, j
character(len=:), allocatable :: word1(:), word2(:)
logical :: result
! Example 1
allocate(word1(2))
allocate(word2(2))
word1(1) = 'ab '
word1(2) = 'c '
word2(1) = 'a '
word2(2) = 'bc '
result = representSameString(word1, word2)
print *, result
! Example 2
deallocate(word1)
deallocate(word2)
allocate(word1(2))
allocate(word2(2))
word1(1) = 'a '
word1(2) = 'cb '
word2(1) = 'ab '
word2(2) = 'c '
result = representSameString(word1, word2)
print *, result
! Example 3
deallocate(word1)
deallocate(word2)
allocate(word1(3))
allocate(word2(1))
word1(1) = 'abc '
word1(2) = 'd '
word1(3) = 'defg '
word2(1) = 'abcddefg '
result = representSameString(word1, word2)
print *, result
contains
function representSameString(word1, word2) result(same)
implicit none
character(len=*), intent(in) :: word1(:), word2(:)
logical :: same
integer :: i, j
character(len=:), allocatable :: concatWord(:)
allocate(concatWord(sum(len(word1))))
concatWord = ''
do i = 1, size(word1)
concatWord = trim(concatWord) // trim(word1(i))
end do
do i = 1, size(word2)
concatWord = trim(concatWord) // trim(word2(i))
end do
same = (concatWord == word2(1))
end function representSameString
end program main
temp.f95:9:13: 9 | allocate(word1(2)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:10:13: 10 | allocate(word2(2)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:21:13: 21 | allocate(word1(2)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:22:13: 22 | allocate(word2(2)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:33:13: 33 | allocate(word1(3)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:34:13: 34 | allocate(word2(1)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:52:17: 52 | allocate(concatWord(sum(len(word1)))) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:55:30: 55 | concatWord = trim(concatWord) // trim(word1(i)) | 1 Error: โstringโ argument of โtrimโ intrinsic at (1) must be a scalar temp.f95:58:30: 58 | concatWord = trim(concatWord) // trim(word2(i)) | 1 Error: โstringโ argument of โtrimโ intrinsic at (1) must be a scalar temp.f95:60:8: 60 | same = (concatWord == word2(1)) | 1 Error: Incompatible ranks 0 and 1 in assignment at (1)
program solve
implicit none
integer, parameter :: max_length = 100
character(len=max_length) :: word1(max_length), word2(max_length)
integer :: i, j, k
! Read input
read (*, *) word1
read (*, *) word2
! Initialize variables
i = 1
j = 1
k = 1
! Check if the two arrays represent the same string
do while (i <= size(word1) .and. j <= size(word2))
if (word1(i) == word2(j)) then
i = i + 1
j = j + 1
else
k = k + 1
end if
end do
! Check if we have reached the end of both arrays
if (i > size(word1) .and. j > size(word2)) then
print *, "true"
else
print *, "false"
end if
end program solve
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7c32df5d7960 in ??? #1 0x7c32df5d84d9 in ??? #2 0x7c32df82c17b in ??? #3 0x7c32df825684 in ??? #4 0x7c32df8262aa in ??? #5 0x7c32df82bb7a in ??? #6 0x594a01596292 in MAIN__ #7 0x594a0159655d in main
def min_num_operations(nums):
result = 0
highest_bit = [0] * 32
for num in nums:
for j in range(32):
if num & (1 << j):
highest_bit[j] += 1
for count in highest_bit:
result = max(result, count)
return result + 31
The algorithm starts by initializing a result variable to 0 and an array called highestBit of size 32 with all elements set to 0. Then, for each number in the input nums array, we perform bitwise operations to check if a specific bit is turned on (1). If so, we increment the corresponding element in the highestBit array by 1. After processing all the numbers in the input array, we loop through the highestBit array and update the result variable to the maximum value found plus 31. Finally, we return the result. The algorithm tries to find the highest bit set in any of the input numbers to determine the minimum number of calls needed to make nums from arr.
int minNumOperations(vector<int>& nums) {
int result = 0;
int highestBit[32] = {0};
for (int num : nums) {
for (int j = 0; j < 32; ++j) {
if (num & (1 << j)) {
highestBit[j]++;
}
}
}
for (int count : highestBit) {
result = max(result, count);
}
return result + 31;
}
The algorithm starts by initializing a result variable to 0 and an array called highestBit of size 32 with all elements set to 0. Then, for each number in the input nums array, we perform bitwise operations to check if a specific bit is turned on (1). If so, we increment the corresponding element in the highestBit array by 1. After processing all the numbers in the input array, we loop through the highestBit array and update the result variable to the maximum value found plus 31. Finally, we return the result. The algorithm tries to find the highest bit set in any of the input numbers to determine the minimum number of calls needed to make nums from arr.