Find the Least Frequent Character in a String

Quick Overview

Return the character with the smallest positive frequency in a lowercase string, breaking ties by whichever character appears first, and return an empty string for empty input. It tests frequency counting with a fixed-size array and a second pass that applies the tie-break rule correctly.

Find the Least Frequent Character in a String

Company: EXL

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` of lowercase English letters, return the character that appears in `s` with the smallest positive frequency. Only characters that actually occur in `s` are considered. A character absent from `s` is never selected. If several characters share the smallest frequency, return the one whose first occurrence in `s` comes earliest (reading `s` from left to right). This is not necessarily the lexicographically smallest character. If `s` is empty, return an empty string `""`. ### Function Signature ```python def least_frequent_character(s: str) -> str: ``` ### Rules - The return value is a string of length 1, or `""` when `s` is empty. - Frequencies are counted over the whole string. Letters with a count of zero are ignored. - Ties on the smallest count go to the character whose first index in `s` is smallest. ### Constraints - `0 <= len(s) <= 10^5` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "zzyaab" Output: "y" ``` The counts are z=2, y=1, a=2, b=1. The smallest positive frequency is 1, shared by `y` and `b`, and `y` appears first (index 2, while `b` is at index 5), so the answer is `y` even though `b` comes first alphabetically. ```text Input: s = "programming" Output: "p" ``` The counts are p=1, r=2, o=1, g=2, a=1, m=2, i=1, n=1. Several letters appear once, and `p` is the first of them in `s`. ```text Input: s = "ccbbbaa" Output: "c" ``` The counts are c=2, b=3, a=2, so the smallest positive frequency is 2. Both `c` and `a` have it, and `c` occurs first at index 0.

Overview: Return the character with the smallest positive frequency in a lowercase string, breaking ties by whichever character appears first, and return an empty string for empty input. It tests frequency counting with a fixed-size array and a second pass that applies the tie-break rule correctly.

EXL logo
EXL
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s of lowercase English letters, return the character that appears in s with the smallest positive frequency. Only characters that actually occur in s are considered. A character absent from s is never selected.

If several characters share the smallest frequency, return the one whose first occurrence in s comes earliest (reading s from left to right). This is not necessarily the lexicographically smallest character.

If s is empty, return an empty string "".

Function Signature

def least_frequent_character(s: str) -> str:

Rules

  • The return value is a string of length 1, or "" when s is empty.
  • Frequencies are counted over the whole string. Letters with a count of zero are ignored.
  • Ties on the smallest count go to the character whose first index in s is smallest.

Constraints

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

Examples

Input: s = "zzyaab"
Output: "y"

The counts are z=2, y=1, a=2, b=1. The smallest positive frequency is 1, shared by y and b, and y appears first (index 2, while b is at index 5), so the answer is y even though b comes first alphabetically.

Input: s = "programming"
Output: "p"

The counts are p=1, r=2, o=1, g=2, a=1, m=2, i=1, n=1. Several letters appear once, and p is the first of them in s.

Input: s = "ccbbbaa"
Output: "c"

The counts are c=2, b=3, a=2, so the smallest positive frequency is 2. Both c and a have it, and c occurs first at index 0.