Given a string s
consisting of lowercase English letters, return the first letter to appear twice.
Note:
a
appears twice before another letter b
if the second occurrence of a
is before the second occurrence of b
.s
will contain at least one letter that appears twice.Example 1:
Input: s = "abccbaacz " Output: "c " Explanation: The letter 'a' appears on the indexes 0, 5 and 6. The letter 'b' appears on the indexes 1 and 4. The letter 'c' appears on the indexes 2, 3 and 7. The letter 'z' appears on the index 8. The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
Example 2:
Input: s = "abcdd " Output: "d " Explanation: The only letter that appears twice is 'd' so we return 'd'.
Constraints:
2 <= s.length <= 100
s
consists of lowercase English letters.s
has at least one repeated letter.program main
implicit none
character(len=100) :: s
integer :: i, j, n
logical :: found
! read input
read(*,*) s
! find first repeated letter
do i = 1, len_trim(s)-1
found = .false.
do j = i+1, len_trim(s)
if (s(i:i) == s(j:j)) then
found = .true.
exit
end if
end do
if (found) then
write(*,'(A)') s(i:i)
exit
end if
end do
end program main
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x79f2d323f960 in ??? #1 0x79f2d32404d9 in ??? #2 0x79f2d349417b in ??? #3 0x79f2d348d684 in ??? #4 0x79f2d348e2aa in ??? #5 0x5c1df4d2321c in MAIN__ #6 0x5c1df4d2339f in main
module first_repeated_letter
implicit none
contains
function first_repeated_letter(s) result(first_letter)
character(len=*), intent(in) :: s
character :: first_letter
integer :: i, j
do i = 1, len(s) - 1
do j = i + 1, len(s)
if (s(i:i) == s(j:j)) then
first_letter = s(i:i)
return
end if
end do
end do
end function first_repeated_letter
end module first_repeated_letter
program main
use first_repeated_letter
implicit none
character(len=100) :: s
character :: first_letter
s = "abccbaacz "
first_letter = first_repeated_letter(s)
write (*,*) "First repeated letter in '", s, "' is '", first_letter, "'."
s = "abcdd "
first_letter = first_repeated_letter(s)
write (*,*) "First repeated letter in '", s, "' is '", first_letter, "'."
end program main
temp.f95:7:30: 7 | function first_repeated_letter(s) result(first_letter) | 1 Error: MODULE attribute of ‘first_repeated_letter’ 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:25: 10 | character :: first_letter | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:15: 12 | integer :: i, j | 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:31: 20 | first_letter = s(i:i) | 1 Error: Syntax error in argument list at (1) temp.f95:21:18: 21 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:23:11: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:7: 25 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:3: 27 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:3: 29 | end function first_repeated_letter | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:5: 35 | use first_repeated_letter | 1 Fatal Error: Cannot open module file ‘first_repeated_letter.mod’ for reading at (1): No such file or directory compilation terminated.
def first_letter_to_appear_twice(s: str) -> str:
occurrences = [0] * 26
for c in s:
occurrences[ord(c) - ord('a')] += 1
if occurrences[ord(c) - ord('a')] == 2:
return c
return '?'
The solution in each programming language follows the same algorithm. First, we create an array to store the occurrences of each lowercase English letter. Then, we iterate through the given string, incrementing the count of the current character in the occurrences array. If an element in the occurrences array reaches 2, that means we've found a letter that appears twice, so we return that letter.
char firstLetterToAppearTwice(std::string s) {
std::array<int, 26> occurrences;
occurrences.fill(0);
for (char c : s) {
occurrences[c - 'a']++;
if (occurrences[c - 'a'] == 2) {
return c;
}
}
return '?';
}
The solution in each programming language follows the same algorithm. First, we create an array to store the occurrences of each lowercase English letter. Then, we iterate through the given string, incrementing the count of the current character in the occurrences array. If an element in the occurrences array reaches 2, that means we've found a letter that appears twice, so we return that letter.