Remove Duplicate Characters From a String in First-Occurrence Order

Quick Overview

Remove repeated characters from a lowercase string so that each character appears once, keeping the order of first occurrence. The problem tests a single left-to-right scan with a hash set and careful handling of output order.

Remove Duplicate Characters From a String in First-Occurrence Order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s`, remove every repeated character so that each character appears only once. Keep the first occurrence of each character and return the remaining characters in the order they first appear in `s`. For example, the distinct characters of `"programming"` in order of first appearance are `p`, `r`, `o`, `g`, `a`, `m`, `i`, `n`, so the result is `"progamin"`. ### Function Signature ```python def remove_duplicates(s: str) -> str: ... ``` ### Rules - Each character is kept at the position of its first occurrence. Every later copy of it is dropped. - The kept characters stay in the relative order they have in `s`. The output is not sorted. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "programming" Output: "progamin" ``` The first occurrences in order are p, r, o, g, a, m, i, n. The second `r`, the second `m` and the last `g` are dropped. ```text Input: s = "abcabc" Output: "abc" ``` After the first `abc`, every character is a repeat, so nothing else is added. ```text Input: s = "bbbab" Output: "ba" ``` `b` first appears before `a`, so the output is `"ba"`. Sorting the distinct letters would give `"ab"`, which is wrong here.

Overview: Remove repeated characters from a lowercase string so that each character appears once, keeping the order of first occurrence. The problem tests a single left-to-right scan with a hash set and careful handling of output order.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, remove every repeated character so that each character appears only once. Keep the first occurrence of each character and return the remaining characters in the order they first appear in s.

For example, the distinct characters of "programming" in order of first appearance are p, r, o, g, a, m, i, n, so the result is "progamin".

Function Signature

def remove_duplicates(s: str) -> str:
    ...

Rules

  • Each character is kept at the position of its first occurrence. Every later copy of it is dropped.
  • The kept characters stay in the relative order they have in s . The output is not sorted.

Constraints

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

Examples

Input: s = "programming"
Output: "progamin"

The first occurrences in order are p, r, o, g, a, m, i, n. The second r, the second m and the last g are dropped.

Input: s = "abcabc"
Output: "abc"

After the first abc, every character is a repeat, so nothing else is added.

Input: s = "bbbab"
Output: "ba"

b first appears before a, so the output is "ba". Sorting the distinct letters would give "ab", which is wrong here.