First Letter to Appear Twice

🏠 ⬅️ ➡️

Given a string s consisting of lowercase English letters, return the first letter to appear twice.

Note:

  • A letter 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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
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.