Sort Characters by How Often They Appear
Updated
Problem statement
Given a string s, rearrange its characters so that characters that appear more often come first. All copies of a character stay together.
If two characters appear the same number of times, the one with the smaller character code (ASCII value) comes first. So digits come before uppercase letters, and uppercase letters before lowercase ones. Uppercase and lowercase forms are different characters.
Return the rearranged string.
Function
frequencySort(s: String) → StringExamples
Example 1
s = "banana"return = "aaannb"a appears 3 times, n 2 times and b once, so the answer is aaa, nn, b.
Example 2
s = "dcba"return = "abcd"All four characters appear once, so the tie rule decides: smallest character code first, which gives abcd.
Example 3
s = "zZz9Z9"return = "99ZZzz"9, Z and z each appear twice. Digits come before uppercase letters, and uppercase before lowercase, so the order is 99, ZZ, zz.
Constraints
1 <= s.length <= 5000scontains only English letters (uppercase and lowercase) and digits
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Top N Frequent Words in a FileMeta · PHONE SCREENMeta interview questions
- Top K Keys from JSON RecordsCapital One · ONSITE INTERVIEWCapital One interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Group AnagramsWorkday · PHONE SCREEN
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions