Longest Uncommon Subsequence I

🏠 ⬅️ ➡️

Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between two strings is a string that is a subsequence of one but not the other.

A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.

  • For example, "abc " is a subsequence of "aebdc " because you can delete the underlined characters in "aebdc " to get "abc ". Other subsequences of "aebdc " include "aebdc ", "aeb ", and " " (empty string).

Example 1:

Input: a = "aba ", b = "cdc " Output: 3 Explanation: One longest uncommon subsequence is "aba " because "aba " is a subsequence of "aba " but not "cdc ". Note that "cdc " is also a longest uncommon subsequence.

Example 2:

Input: a = "aaa ", b = "bbb " Output: 3 Explanation: The longest uncommon subsequences are "aaa " and "bbb ".

Example 3:

Input: a = "aaa ", b = "aaa " Output: -1 Explanation: Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a.

Constraints:

  • 1 <= a.length, b.length <= 100
  • a and b consist of lower-case English letters.

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

    character(len=100) :: a, b
    integer :: n

    call get_command_argument(1, a)
    call get_command_argument(2, b)

    n = longest_uncommon_subsequence(a, b)
    write (*, '(I0)') n

contains

    function longest_uncommon_subsequence(a, b) result(n)
        implicit none
        character(len=*), intent(in) :: a, b
        integer :: i, j, k, l, m, n
        logical :: found

        ! Initialize variables
        n = -1
        found = .false.

        ! Loop over all possible starting indices for a
        do i = 1, len(a)
            ! Loop over all possible ending indices for a
            do j = i, len(a)
                ! Check if a(i:j) is a subsequence of b
                do k = 1, len(b)
                    ! Check if b(k:) is a subsequence of a(i:j)
                    do l = k, len(b)
                        if (a(i:j) == b(k:l)) then
                            ! If it is, check if a(i:j) is longer than the current longest uncommon subsequence
                            if (len(a(i:j)) > n) then
                                ! If it is, update the longest uncommon subsequence
                                n = len(a(i:j))
                                found = .true.
                            end if
                            exit
                        end if
                    end do
                    if (found) exit
                end do
                if (found) exit
            end do
            if (found) exit
        end do

    end function longest_uncommon_subsequence

end program main
Compiled
Executed
Correct
!-------------------------------------------------------------------------------
  !
  ! Fortran script to solve the longest uncommon subsequence problem
  !
  !-------------------------------------------------------------------------------

  program longest_uncommon_subsequence

    implicit none

    character(len=100) :: a, b
    integer :: i, j, k, l, m, n
    integer :: max_len
    integer :: len_a, len_b
    integer :: dp(100, 100)

    ! Read input
    read(*,*) a
    read(*,*) b

    ! Initialize dp array
    do i = 1, 100
        do j = 1, 100
            dp(i, j) = -1
        end do
    end do

    ! Set the length of the input strings
    len_a = len(a)
    len_b = len(b)

    ! Initialize the maximum length
    max_len = -1

    ! Loop through all possible substrings of a
    do i = 1, len_a
        do j = i, len_a
            ! Loop through all possible substrings of b
            do k = 1, len_b
                do l = k, len_b
                    ! Check if the current substring is a subsequence of the other
                    if (index(a(i:j), b(k:l)) /= 0 .or. index(b(k:l), a(i:j)) /= 0) then
                        ! If it is, set the maximum length to the current length
                        max_len = max(max_len, j-i+1)
                    end if
                end do
            end do
        end do
    end do

    ! Print the maximum length
    write(*,*) max_len

  end program longest_uncommon_subsequence
🌐 Data from online sources
def findLUSlength(a: str, b: str) -> int:
    if a == b:
        return -1
    return max(len(a), len(b))

The algorithm is as follows: 1. First, we compare if the strings a and b are equal. If they are equal, it means that there is no uncommon subsequence between them, so we return -1. 2. If the strings are not equal, then we find the length of both strings and return the one with the maximum length. This is because the longest uncommon subsequence can be the longer of the two strings itself (since it is not a subsequence of the other string).

🌐 Data from online sources
int findLUSlength(string a, string b) {
    if (a == b) {
        return -1;
    }
    return max(a.length(), b.length());
}

The algorithm is as follows: 1. First, we compare if the strings a and b are equal. If they are equal, it means that there is no uncommon subsequence between them, so we return -1. 2. If the strings are not equal, then we find the length of both strings and return the one with the maximum length. This is because the longest uncommon subsequence can be the longer of the two strings itself (since it is not a subsequence of the other string).