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.
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.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
temp.f95:19:24: 19 | function can_become(s, goal) result(res) | 1 Error: Procedure โcan_becomeโ at (1) has an explicit interface from a previous declaration temp.f95:20:21: 20 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:21:47: 21 | character(len=*), intent(in) :: s, goal | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:22:23: 22 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:22: 23 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:21: 25 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:29: 26 | do while (i < len(s)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:27:37: 27 | if (s(i+1:) == goal) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:28:28: 28 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:20: 29 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:30:15: 30 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:21: 33 | i = i + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:11: 34 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:35:7: 35 | end function can_become | 1 Error: Expecting END PROGRAM statement at (1)
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
temp.f95:42:12: 42 | s(1:n-1) = s(2:n) | 1 Error: Dummy argument โsโ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:43:12: 43 | s(n:n) = c | 1 Error: Dummy argument โsโ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:56:5: 56 | use shift_strings | 1 Fatal Error: Cannot open module file โshift_strings.modโ for reading at (1): No such file or directory compilation terminated.
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.
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.