Skip to main content

Shuffle String — UnStop sample question




 Problem Statement

Mr. Agoji is given a shuffled string made by randomly shuffling a special string. A sting will be called special only if it is formed by joining some special words any number of times. Special words are mapping of numbers (0 <= number < 10) to their words, for example, mapping of ‘0’ to ‘zero’, mapping of ‘1’ to ‘one’, and so on. Mr. Agoji is asked to convert the shuffled string to the smallest special number. A special number is a number formed using numbers (0 <= number < 10) without any leading zeroes. Mr. Agoji being not so good with numbers and strings ask for your help.

Input Format

The first line of the input will contain T, the number of test cases, 1 <= T <= 100 For each test case, There will be a, s shuffled string, on a separate line

1 <= s.length <= 100000

Output Format

For each test case, on the new line, the Smallest special number is in the string format. Some notes on output: Shuffled string will always be able to convert into at least one valid special string. Shuffled string will only contain small English alphabets. If a shuffled string contains only zeroes, you should output “0”.

Approach:

  1. create a map of digit to words
  2. create frequency map of characters from input string
  3. Traverse all digits from 0 to 9 and see how many numbers are represented in string. save it in digit_freq dictionary
  4. sort all numbers in digit_freq and create a string from that
  5. There are few edge case we need to take care. If all are zero then return 0. Another edge case is we cant keep 0 in front positions of the result string. we can keep all zeros after first non zero digit to create smallest number according to requirement.

Python Code:

Java Code:

This is sample question given for Codegoda 2023 code Challenge. I didn’t get chance to test all testcases. Let me know if you can’t submit this code by any chance.




Comments

Popular posts from this blog

Will job market get better in 2023?

  The job market is a crucial aspect of any economy, and it can significantly impact the standard of living for individuals and families. After the COVID-19 pandemic, the job market was hit hard, and many people lost their jobs, leading to economic uncertainty and hardship. However, as we move into 2023, the question on many people's minds is whether the job market will improve. In this blog, we will explore the factors that could impact the job market in 2023 and provide insights into what we can expect. Factors that could impact the job market in 2023: Economic Growth Economic growth is a crucial factor that affects the job market. As the economy grows, companies are more likely to expand and hire more employees. The International Monetary Fund (IMF) projects that the global economy will grow by 4.9% in 2022, which could translate into more job opportunities in various industries. COVID-19 The COVID-19 pandemic has significantly impacted the job market in the last two years, and ...