You are given a 0-indexed string s
consisting of only lowercase English letters, where each letter in s
appears exactly twice. You are also given a 0-indexed integer array distance
of length 26
.
Each letter in the alphabet is numbered from 0
to 25
(i.e. 'a' -> 0
, 'b' -> 1
, 'c' -> 2
, ... , 'z' -> 25
).
In a well-spaced string, the number of letters between the two occurrences of the ith
letter is distance[i]
. If the ith
letter does not appear in s
, then distance[i]
can be ignored.
Return true
if s
is a well-spaced string, otherwise return false
.
Example 1:
Input: s = "abaccb ", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: true Explanation: - 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1. - 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3. - 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0. Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored. Return true because s is a well-spaced string.
Example 2:
Input: s = "aa ", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] Output: false Explanation: - 'a' appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string.
Constraints:
2 <= s.length <= 52
s
consists only of lowercase English letters.s
exactly twice.distance.length == 26
0 <= distance[i] <= 50
program main
implicit none
character(len=52) :: s
integer, dimension(26) :: distance
logical :: result
! Examples taken from the problem statement
s = "abaccb "
distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
result = wellSpacedString(s, distance)
write (*,*) result
s = "aa "
distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
result = wellSpacedString(s, distance)
write (*,*) result
contains
logical function wellSpacedString(s, distance)
implicit none
character(len=*), intent(in) :: s
integer, dimension(:), intent(in) :: distance
integer :: i, j, k
wellSpacedString = .true.
do i = 0, 25
if (s(i+1:i+1) /= '') then
j = index(s, s(i+1:i+1))
k = index(s, s(i+1:i+1), back=.true.)
if (j > k) then
wellSpacedString = .false.
exit
end if
if (k - j < distance(i)) then
wellSpacedString = .false.
exit
end if
end if
end do
end function wellSpacedString
end program main
temp.f95:10:4: 10 | distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (26 and 25) temp.f95:15:4: 15 | distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (26 and 25)
module well_spaced_string
implicit none
contains
function is_well_spaced_string(s, distance) result(is_well_spaced)
! Given a string s consisting of only lowercase English letters, where each letter appears exactly twice.
! Given a 0-indexed integer array distance of length 26.
! Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ..., 'z' -> 25).
! In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i].
! If the ith letter does not appear in s, then distance[i] can be ignored.
! Return true if s is a well-spaced string, otherwise return false.
character(len=*), intent(in) :: s
integer, intent(in) :: distance(:)
logical :: is_well_spaced
! Edge case: If the string is empty, return true
if (len(trim(s)) == 0) then
is_well_spaced = .true.
return
end if
! Initialize the indices of the first and second occurrences of each letter
integer :: first_index(26), second_index(26)
first_index = -1
second_index = -1
! Loop through the string and keep track of the indices of the first and second occurrences of each letter
do i = 1, len(s)
if (first_index(iachar(s(i:i))) == -1) then
first_index(iachar(s(i:i))) = i
else
second_index(iachar(s(i:i))) = i
end if
end do
! Check if the distance between the two occurrences of each letter is valid
do i = 1, 26
if (first_index(i) == -1 .or. second_index(i) == -1) then
! If a letter does not appear in the string, the distance can be ignored
cycle
else if (second_index(i) - first_index(i) == distance(i)) then
! If the distance between the two occurrences of the letter is valid, continue
cycle
else
! If the distance between the two occurrences of the letter is not valid, return false
is_well_spaced = .false.
return
end if
end do
! If we reach this point, the string is a well-spaced string
is_well_spaced = .true.
end function is_well_spaced_string
end module well_spaced_string
! Test the function with the given examples
program test_well_spaced_string
use well_spaced_string
implicit none
! Examples from the problem statement
character(len=10) :: s
integer :: distance(26)
logical :: is_well_spaced
! Example 1
s = "abaccb "
distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
is_well_spaced = is_well_spaced_string(s, distance)
write (*,*) "Example 1: ", is_well_spaced
! Example 2
s = "aa "
distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
is_well_spaced = is_well_spaced_string(s, distance)
write (*,*) "Example 2: ", is_well_spaced
end program test_well_spaced_string
temp.f95:27:44: 27 | integer :: first_index(26), second_index(26) | 1 Error: Unexpected data declaration statement at (1) temp.f95:33:20: 33 | if (first_index(iachar(s(i:i))) == -1) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:35:8: 35 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:37:7: 37 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:42:20: 42 | if (first_index(i) == -1 .or. second_index(i) == -1) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:45:26: 45 | else if (second_index(i) - first_index(i) == distance(i)) then | 1 Error: Missing ‘)’ in ELSE IF expression at (1) temp.f95:48:8: 48 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:52:7: 52 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:28:11: 28 | first_index = -1 | 1 Error: Symbol ‘first_index’ at (1) has no IMPLICIT type temp.f95:32:4: 32 | do i = 1, len(s) | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:29:12: 29 | second_index = -1 | 1 Error: Symbol ‘second_index’ at (1) has no IMPLICIT type temp.f95:65:5: 65 | use well_spaced_string | 1 Fatal Error: Cannot open module file ‘well_spaced_string.mod’ for reading at (1): No such file or directory compilation terminated.
def well_spaced_string(s: str, distance: List[int]) -> bool:
first = {}
for i, c in enumerate(s):
if c not in first:
first[c] = i
else:
if distance[ord(c) - ord('a')] != i - first[c] - 1:
return False
return True
The algorithm iterates through the characters of the given string s
. If it encounters a character for the first time, it stores the character along with its index in a map first
. If it encounters the character for the second time, it checks whether the difference between the current index and the first occurrence of the character minus 1 is equal to the required distance given in the distance
array. If this condition is not met, the function returns false
. If the iteration is completed without failing the condition, the function returns true
.
bool wellSpacedString(std::string s, std::vector<int> distance) {
std::unordered_map<char, int> first;
for (int i = 0; i < s.size(); ++i) {
char c = s[i];
if (first.find(c) == first.end()) {
first[c] = i;
} else {
if (distance[c - 'a'] != i - first[c] - 1) {
return false;
}
}
}
return true;
}
The algorithm iterates through the characters of the given string s
. If it encounters a character for the first time, it stores the character along with its index in a map first
. If it encounters the character for the second time, it checks whether the difference between the current index and the first occurrence of the character minus 1 is equal to the required distance given in the distance
array. If this condition is not met, the function returns false
. If the iteration is completed without failing the condition, the function returns true
.