Check If Two String Arrays are Equivalent

๐Ÿ  โฌ…๏ธ โžก๏ธ

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.

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
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
โœ… Compiled
โŒ Executed
โŒ Correct
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
๐ŸŒ Data from online sources
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.

๐ŸŒ Data from online sources
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.