Move every '#' to the front of a string and keep the other characters in order

Quick Overview

Rearrange a string of letters, digits and hash symbols so every hash symbol comes first while the other characters keep their original order. It tests stable partitioning, building a string in linear time, and the right-to-left two-pointer technique for doing it in place.

Move every '#' to the front of a string and keep the other characters in order

Company: HCL Technologies

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` of length `n` containing alphanumeric characters and `'#'` symbols, return a new string where all `'#'` characters are moved to the beginning of the string. The relative order of all non-`'#'` characters must be preserved. ### Function Signature ```python def move_hash_to_front(s: str) -> str: ``` ### Rules - The result has the same length as `s` and contains the same characters. - Every `'#'` comes before every other character, and the non-`'#'` characters appear in the same order as in `s`. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of alphanumeric characters and `'#'` only. ### Examples ```text Input: s = "a#bc#d" Output: "##abcd" ``` There are 2 `'#'` characters. Moving them to the front while keeping 'a', 'b', 'c', 'd' in order gives "##abcd". ```text Input: s = "###abc" Output: "###abc" ``` All `'#'` characters are already at the front, so the string stays the same. ```text Input: s = "1#a2##" Output: "###1a2" ``` The three `'#'` characters go first, followed by '1', 'a' and '2' in their original order.

Overview: Rearrange a string of letters, digits and hash symbols so every hash symbol comes first while the other characters keep their original order. It tests stable partitioning, building a string in linear time, and the right-to-left two-pointer technique for doing it in place.

HCL Technologies logo
HCL Technologies
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s of length n containing alphanumeric characters and '#' symbols, return a new string where all '#' characters are moved to the beginning of the string.

The relative order of all non-'#' characters must be preserved.

Function Signature

def move_hash_to_front(s: str) -> str:

Rules

  • The result has the same length as s and contains the same characters.
  • Every '#' comes before every other character, and the non- '#' characters appear in the same order as in s .

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of alphanumeric characters and '#' only.

Examples

Input: s = "a#bc#d"
Output: "##abcd"

There are 2 '#' characters. Moving them to the front while keeping 'a', 'b', 'c', 'd' in order gives "##abcd".

Input: s = "###abc"
Output: "###abc"

All '#' characters are already at the front, so the string stays the same.

Input: s = "1#a2##"
Output: "###1a2"

The three '#' characters go first, followed by '1', 'a' and '2' in their original order.