Largest Substring Between Two Equal Characters

🏠 ⬅️ ➡️

Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.

A substring is a contiguous sequence of characters within a string.

Example 1:

Input: s = "aa " Output: 0 Explanation: The optimal substring here is an empty substring between the two 'a's.

Example 2:

Input: s = "abca " Output: 2 Explanation: The optimal substring here is "bc ".

Example 3:

Input: s = "cbzxy " Output: -1 Explanation: There are no characters that appear twice in s.

Constraints:

  • 1 <= s.length <= 300
  • s contains only lowercase English letters.

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

    character(len=:), allocatable :: s
    integer :: i, j, k, l, m, n

    ! Example 1
    s = 'aa '
    write (*,*) 'Example 1:'
    write (*,*) 'Input: ', trim(s)
    write (*,*) 'Output: ', longest_substring(s)
    write (*,*) ''

    ! Example 2
    s = 'abca '
    write (*,*) 'Example 2:'
    write (*,*) 'Input: ', trim(s)
    write (*,*) 'Output: ', longest_substring(s)
    write (*,*) ''

    ! Example 3
    s = 'cbzxy '
    write (*,*) 'Example 3:'
    write (*,*) 'Input: ', trim(s)
    write (*,*) 'Output: ', longest_substring(s)
    write (*,*) ''

contains

    function longest_substring(s) result(res)
        implicit none
        character(len=*), intent(in) :: s
        integer :: res

        integer :: i, j, k, l, m, n

        do i = 1, len(s) - 1
            do j = i + 1, len(s)
                if (s(i:j) == s(i+1:j+1)) then
                    res = max(res, j - i)
                end if
            end do
        end do

        if (res == 0) then
            res = -1
        end if

    end function longest_substring

end program main
Compiled
Executed
Correct
module longest_substring

implicit none

contains

function longest_substring(s) result(ans)

character(len=*), intent(in) :: s
integer :: ans

integer :: i, j, count

do i = 1, len(s) - 1

    do j = i + 1, len(s)

        if (s(i:i) == s(j:j)) then

            count = 0

            do while (s(i + count:i + count) == s(j + count:j + count))

                count = count + 1

            end do

            ans = max(ans, count - 1)

        end if

    end do

end do

end function longest_substring

end module

program test_longest_substring

use longest_substring

implicit none

character(len=300) :: s
integer :: ans

s = "aa "
ans = longest_substring(s)
write (*,*) "The longest substring between two equal characters in '", s, "' is ", ans

s = "abca "
ans = longest_substring(s)
write (*,*) "The longest substring between two equal characters in '", s, "' is ", ans

s = "cbzxy "
ans = longest_substring(s)
write (*,*) "The longest substring between two equal characters in '", s, "' is ", ans

end program
🌐 Data from online sources
def maxLengthBetweenEqualCharacters(s: str) -> int:
    result = -1
    char_map = {}

    for i, c in enumerate(s):
        if c not in char_map:
            char_map[c] = i
        else:
            result = max(result, i - char_map[c] - 1)

    return result

The algorithm uses a hash map to store the index of the first occurrence of each character in the string. It then iterates through the string, and for each character, checks if it's present in the hash map. If it's not, the character is added to the hash map with its index. If it is present, the length of the substring between the two occurrences of the character is calculated as (current index - stored index - 1). The result is updated as the maximum length between equal characters found so far.

🌐 Data from online sources
int maxLengthBetweenEqualCharacters(std::string s) {
    int result = -1;
    std::unordered_map<char, int> char_map;

    for (int i = 0; i < s.length(); ++i) {
        if (char_map.find(s[i]) == char_map.end()) {
            char_map[s[i]] = i;
        } else {
            result = std::max(result, i - char_map[s[i]] - 1);
        }
    }
    return result;
}

The algorithm uses a hash map to store the index of the first occurrence of each character in the string. It then iterates through the string, and for each character, checks if it's present in the hash map. If it's not, the character is added to the hash map with its index. If it is present, the length of the substring between the two occurrences of the character is calculated as (current index - stored index - 1). The result is updated as the maximum length between equal characters found so far.