Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.

A shift on s consists of moving the leftmost character of s to the rightmost position.

  • For example, if s = "abcde ", then it will be "bcdea " after one shift.

Example 1:

Input: s = "abcde", goal = "cdeab" Output: true

Example 2:

Input: s = "abcde", goal = "abced" Output: false

Constraints:

  • 1 <= s.length, goal.length <= 100
  • s and goal consist of lowercase English letters.

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

    character(len=100) :: s, goal
    logical :: can_become

    ! Example 1
    s = "abcde"
    goal = "cdeab"
    print '(L1)', can_become(s, goal)

    ! Example 2
    s = "abcde"
    goal = "abced"
    print '(L1)', can_become(s, goal)

contains

    function can_become(s, goal) result(res)
        implicit none
        character(len=*), intent(in) :: s, goal
        integer :: i, j
        logical :: res

        res = .false.
        do while (i < len(s))
            if (s(i+1:) == goal) then
                res = .true.
                exit
            end if
            s(1:) = s(2:)
            s(len_trim(s):) = s(1:)
            i = i + 1
        end do
    end function can_become
end program main
โŒ Compiled
โŒ Executed
โŒ Correct
module shift_strings

implicit none

contains

function can_shift(s, goal) result(output)

character(len=*), intent(in) :: s, goal
logical :: output

integer :: i, j, n
character(len=1) :: c

output = .false.
n = len(s)

! Check if the length of the strings is the same
if (n /= len(goal)) then
    return
end if

! Check if the strings are equal
if (s == goal) then
    output = .true.
    return
end if

! Check if the strings can be shifted to the same length
do i = 1, n - 1
    if (s(i:i+1) == goal(i:i+1)) then
        output = .true.
        return
    end if
end do

! Check if the strings can be shifted to the same length by shifting the leftmost character
do i = 1, n - 1
    c = s(1:1)
    do j = 1, n - 1
        if (s(j+1:j+2) == goal(j:j+1)) then
            s(1:n-1) = s(2:n)
            s(n:n) = c
            output = .true.
            return
        end if
    end do
end do

end function can_shift

end module shift_strings

program test_shift_strings

use shift_strings

implicit none

integer :: i, j
character(len=100) :: s, goal
logical :: output

! Test case 1:
s = "abcde"
goal = "cdeab"
output = can_shift(s, goal)
if (output) then
    write (*,*) "Test case 1: Pass"
else
    write (*,*) "Test case 1: Fail"
end if

! Test case 2:
s = "abcde"
goal = "abced"
output = can_shift(s, goal)
if (.not. output) then
    write (*,*) "Test case 2: Pass"
else
    write (*,*) "Test case 2: Fail"
end if

! Test case 3:
s = "abc"
goal = "cab"
output = can_shift(s, goal)
if (output) then
    write (*,*) "Test case 3: Pass"
else
    write (*,*) "Test case 3: Fail"
end if

! Test case 4:
s = "abc"
goal = "acb"
output = can_shift(s, goal)
if (.not. output) then
    write (*,*) "Test case 4: Pass"
else
    write (*,*) "Test case 4: Fail"
end if

! Test case 5:
s = "a"
goal = "a"
output = can_shift(s, goal)
if (output) then
    write (*,*) "Test case 5: Pass"
else
    write (*,*) "Test case 5: Fail"
end if

! Test case 6:
s = "a"
goal = "b"
output = can_shift(s, goal)
if (.not. output) then
    write (*,*) "Test case 6: Pass"
else
    write (*,*) "Test case 6: Fail"
end if

! Test case 7:
s = "ab"
goal = "ba"
output = can_shift(s, goal)
if (output) then
    write (*,*) "Test case 7: Pass"
else
    write (*,*) "Test case 7: Fail"
end if

! Test case 8:
s = "ab"
goal = "ac"
output = can_shift(s, goal)
if (.not. output) then
    write (*,*) "Test case 8: Pass"
else
    write (*,*) "Test case 8: Fail"
end if

! Test case 9:
s = "abc"
goal = "bca"
output = can_shift(s, goal)
if (output) then
    write (*,*) "Test case 9: Pass"
else
    write
๐ŸŒ Data from online sources
def can_transform(sx, sy, tx, ty):
    while sx != tx or sy != ty:
        if sx > tx or sy > ty:
            return False
        if sx < tx:
            tx -= ty
        else:
            ty -= sx
    return True

The algorithm starts with the target coordinates (tx, ty), and works its way backward by performing the inverse operations to approach the source point (sx, sy). In each iteration, check if sx > tx or sy > ty, meaning the target point cannot be reached (return false). Depending on whether tx > sx or ty > sy, subtract the smaller coordinate from the larger one. If (tx and ty) become equal to (sx and sy), the target point can be reached, and the function returns true.

๐ŸŒ Data from online sources
bool canTransform(int sx, int sy, int tx, int ty) {
    while (sx != tx || sy != ty) {
        if (sx > tx || sy > ty) return false;
        if (sx < tx) {
            tx -= ty;
        } else {
            ty -= sx;
        }
    }
    return true;
}

The algorithm starts with the target coordinates (tx, ty), and works its way backward by performing the inverse operations to approach the source point (sx, sy). In each iteration, check if sx > tx or sy > ty, meaning the target point cannot be reached (return false). Depending on whether tx > sx or ty > sy, subtract the smaller coordinate from the larger one. If (tx and ty) become equal to (sx and sy), the target point can be reached, and the function returns true.