Greatest Common Divisor of Strings

🏠 ⬅️ ➑️

For two strings s and t, we say "t divides s " if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

Example 1:

Input: str1 = "ABCABC ", str2 = "ABC " Output: "ABC "

Example 2:

Input: str1 = "ABABAB ", str2 = "ABAB " Output: "AB "

Example 3:

Input: str1 = "LEET ", str2 = "CODE " Output: " "

Constraints:

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of English uppercase letters.

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

    character(len=:), allocatable :: str1, str2, x
    integer :: i

    ! Example 1
    str1 = "ABCABC "
    str2 = "ABC "
    call solve(str1, str2, x)
    write (*, '(A)') trim(x)

    ! Example 2
    str1 = "ABABAB "
    str2 = "ABAB "
    call solve(str1, str2, x)
    write (*, '(A)') trim(x)

    ! Example 3
    str1 = "LEET "
    str2 = "CODE "
    call solve(str1, str2, x)
    write (*, '(A)') trim(x)

contains

    subroutine solve(str1, str2, x)
        implicit none
        character(len=*), intent(in) :: str1, str2
        character(len=:), allocatable, intent(out) :: x
        integer :: n

        n = len(str2)
        do while (n > 0)
            if (index(str1, str2) /= 0 .and. index(str2, str2) == 0) then
                x = str2
                exit
            end if
            n = n - 1
            str2 = str2 // str2
        end do
    end subroutine solve

end program main
❌ Compiled
❌ Executed
❌ Correct
program longest_common_divisor
      implicit none

      character(len=1000) :: str1, str2, x
      integer :: i, j, k, l, m, n

      read(*,*) str1, str2

      ! Initialize x as an empty string
      x = ""

      ! Loop through all possible lengths of x
      do i = 1, len(str1)

         ! Loop through all possible starting indices of x in str1
         do j = 1, len(str1) - i + 1

            ! Loop through all possible lengths of x in str2
            do k = 1, len(str2)

               ! Loop through all possible starting indices of x in str2
               do l = 1, len(str2) - k + 1

                  ! Check if x divides both str1 and str2
                  if (str1(j:j+i-1) == str2(l:l+k-1)) then
                     x = str1(j:j+i-1)
                     exit
                  end if

               end do

               if (x /= "") exit

            end do

            if (x /= "") exit

         end do

         if (x /= "") exit

      end do

      ! Output the largest string x that divides both str1 and str2
      write(*,*) x

      end program longest_common_divisor

This script uses a brute force approach to solve the problem. It loops through all possible lengths of the string `x` and checks if `x` divides both `str1` and `str2`. If `x` divides both strings, it is the largest string that divides both strings. The script uses the `len` function to get the length of the strings and the `==` operator to check if two strings are equal. The `exit` statement is used to exit the loops early if `x` is found to divide both strings.

Note that this script assumes that the input strings are uppercase and contain only English uppercase letters. If the input strings can contain other characters, you may need to modify the script accordingly.
🌐 Data from online sources
def prefixesDivBy5(nums):
    result = []
    current = 0
    for num in nums:
        current = ((current << 1) | num) % 5
        result.append(current == 0)
    return result

The problem requires to find out if the binary representation of the subarray from 0 to i is divisible by 5. To solve this, we can maintain an integer current that keeps track of the value of this binary representation modulo 5.

Iterate through the nums array, and, for each number in the array, update the running total current as follows: - Shift current one bit to the left (multiply by 2) using the bitwise left shift operator << 1 - Perform bitwise OR operation with the current number num to add it as the least significant bit to the binary representation - Compute this binary representation modulo 5

After computing the current binary representation (modulo 5), check if it is divisible by 5. If it's divisible, then the value of current would be 0. Append the result (true or false) to the answer list or vector.

Finally, return the resultant list of booleans.###

End ###

🌐 Data from online sources
#include <vector>

std::vector<bool> prefixesDivBy5(std::vector<int>& nums) {
    std::vector<bool> result;
    int current = 0;
    for (int num : nums) {
        current = ((current << 1) | num) % 5;
        result.push_back(current == 0);
    }
    return result;
}

The problem requires to find out if the binary representation of the subarray from 0 to i is divisible by 5. To solve this, we can maintain an integer current that keeps track of the value of this binary representation modulo 5.

Iterate through the nums array, and, for each number in the array, update the running total current as follows: - Shift current one bit to the left (multiply by 2) using the bitwise left shift operator << 1 - Perform bitwise OR operation with the current number num to add it as the least significant bit to the binary representation - Compute this binary representation modulo 5

After computing the current binary representation (modulo 5), check if it is divisible by 5. If it's divisible, then the value of current would be 0. Append the result (true or false) to the answer list or vector.

Finally, return the resultant list of booleans.###

End ###