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.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
Example 1: Input: aa Output: 32765 Example 2: Input: abca Output: 32765 Example 3: Input: cbzxy Output: 32765
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
temp.f95:7:26: 7 | function longest_substring(s) result(ans) | 1 Error: MODULE attribute of ‘longest_substring’ conflicts with PROCEDURE attribute at (1) temp.f95:9:33: 9 | character(len=*), intent(in) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:14: 10 | integer :: ans | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:22: 12 | integer :: i, j, count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:20: 14 | do i = 1, len(s) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:24: 16 | do j = i + 1, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:18:16: 18 | if (s(i:i) == s(j:j)) then | 1 Error: Syntax error in argument list at (1) temp.f95:20:21: 20 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:34: 22 | do while (s(i + count:i + count) == s(j + count:j + count)) | 1 Error: Syntax error in argument list at (1) temp.f95:24:33: 24 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:15: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:37: 28 | ans = max(ans, count - 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:11: 30 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:7: 32 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:3: 34 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:3: 36 | end function longest_substring | 1 Error: Expecting END MODULE statement at (1) temp.f95:42:5: 42 | use longest_substring | 1 Fatal Error: Cannot open module file ‘longest_substring.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.