Find the first character in reading order that repeats anywhere in a string

Quick Overview

Given a string of lowercase letters, return the first character in left-to-right order whose total count in the string is at least two, or an empty string when no letter repeats. It tests frequency counting with a fixed-size array and a careful reading of which character counts as first.

Find the first character in reading order that repeats anywhere in a string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` of lowercase English letters, return the first character in reading order that appears more than once anywhere in `s`. Scan `s` from left to right and return the first character whose total count in `s` is at least 2. If no character repeats, return an empty string `""`. This is different from returning the character whose second occurrence comes earliest. For `s = "abccba"` this problem returns `"a"`, because `'a'` is the first character read and it occurs again later in the string. A "first letter to appear twice" approach would return `"c"`, because the second `'c'` is the earliest second occurrence. ### Function Signature ```python def first_repeating_character(s: str) -> str: ``` ### Rules - Return the answer as a one-character string. - Among all characters whose total count in `s` is at least 2, return the one whose first occurrence is leftmost. - If every character appears exactly once, return `""`. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "abccba" Output: "a" ``` `'a'` is the first character read, and it appears twice in total, so the answer is `"a"`. The second `'c'` is the earliest second occurrence, but that does not matter here. ```text Input: s = "adgfdcbbgf" Output: "d" ``` `'a'` appears only once. `'d'` is the next character and appears twice, so the answer is `"d"`. ```text Input: s = "abcde" Output: "" ``` Every letter appears exactly once, so no character repeats and the result is the empty string.

Overview: Given a string of lowercase letters, return the first character in left-to-right order whose total count in the string is at least two, or an empty string when no letter repeats. It tests frequency counting with a fixed-size array and a careful reading of which character counts as first.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s of lowercase English letters, return the first character in reading order that appears more than once anywhere in s. Scan s from left to right and return the first character whose total count in s is at least 2. If no character repeats, return an empty string "".

This is different from returning the character whose second occurrence comes earliest. For s = "abccba" this problem returns "a", because 'a' is the first character read and it occurs again later in the string. A "first letter to appear twice" approach would return "c", because the second 'c' is the earliest second occurrence.

Function Signature

def first_repeating_character(s: str) -> str:

Rules

  • Return the answer as a one-character string.
  • Among all characters whose total count in s is at least 2, return the one whose first occurrence is leftmost.
  • If every character appears exactly once, return "" .

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters.

Examples

Input: s = "abccba"
Output: "a"

'a' is the first character read, and it appears twice in total, so the answer is "a". The second 'c' is the earliest second occurrence, but that does not matter here.

Input: s = "adgfdcbbgf"
Output: "d"

'a' appears only once. 'd' is the next character and appears twice, so the answer is "d".

Input: s = "abcde"
Output: ""

Every letter appears exactly once, so no character repeats and the result is the empty string.