BackeasyStringsZomato

Shift Hash Characters Solution

Problem Statement

You are given a string s. Rearrange the characters of s such that all hash characters ('#') are moved to the end of the string, while the relative order of all other characters remains unchanged. Return the modified string.

Example 1
Input
abc#
Output
abc##

Explanation: Step-by-step: with input 'abc#', we first initialize two pointers, one at the beginning of the string and one at the end. We then iterate through the string, moving non-hash characters to the front and hash characters to the end. In this case, we move 'a', 'b', and 'c' to the front, and '#' to the end, resulting in the output 'abc##'.

Example 2
Input
code#
Output
code##

Explanation: Step-by-step: with input 'code#', we first initialize two pointers, one at the beginning of the string and one at the end. We then iterate through the string, moving non-hash characters to the front and hash characters to the end. In this case, we move 'c', 'o', 'd', and 'e' to the front, and '#' to the end, resulting in the output 'code##'.

Constraints

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters and '#' characters.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Shift Hash Characters — Problem Statement & Solution Guide

StringsEasySlow/Fast Pointers
TimeO(n)
|
SpaceO(n)

Problem Description

You are given a string s. Rearrange the characters of s such that all hash characters ('#') are moved to the end of the string, while the relative order of all other characters remains unchanged. Return the modified string.

Examples

Example 1

Input

abc#

Output

abc##

Explanation: Step-by-step: with input 'abc#', we first initialize two pointers, one at the beginning of the string and one at the end. We then iterate through the string, moving non-hash characters to the front and hash characters to the end. In this case, we move 'a', 'b', and 'c' to the front, and '#' to the end, resulting in the output 'abc##'.

Example 2

Input

code#

Output

code##

Explanation: Step-by-step: with input 'code#', we first initialize two pointers, one at the beginning of the string and one at the end. We then iterate through the string, moving non-hash characters to the front and hash characters to the end. In this case, we move 'c', 'o', 'd', and 'e' to the front, and '#' to the end, resulting in the output 'code##'.

Constraints

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters and '#' characters.

Optimal Approach & Strategy

Convert the string to a mutable array and use a two-pointer read/write approach. The write pointer tracks the placement of non-hash characters, which are copied forward sequentially, and the rest of the array is backfilled with hashes in a single pass.

Brute Force Approach

Iterate through the string, and whenever we find a hash character, shift all subsequent characters one position to the left and place the hash at the end of the string. This requires shifting elements repeatedly, leading to an O(n²) time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function shiftHashCharacters(s) { let nonHash = []; let hash = []; for (let char of s) { if (char === '#') { hash.push(char); } else { nonHash.push(char); } } return nonHash.join('') + hash.join(''); }

Asked in Top Tech Interviews

Zomato

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.