Connection Status:
Competition Arena > Scramble
Rookie SRM 13 · 2022-05-16 · by ged · Simulation
Class Name: Scramble
Return Type: String
Method Name: scrambleWord
Arg Types: (string)
Problem Statement

Problem Statement

Did you konw taht slcrabmed wdros can be raed wtih vrey ltlite erfoft? Oh, sorry. Did you know that scrambled words can be read with very little effort? We can interpret most heavily mangled words because our brains "see" the similarity with the original words and catch the meaning of the text. It is claimed that as long as the first and last letters in every word remain unchanged, we can shuffle the rest of the letters within each word to obtain a reasonably readable text. We want to try the following strategy of scrambling letters in words:

  1. Leave the first and last letters in their original positions, and remove the rest of the letters. For example, "alphabet" becomes "a______t" (underscores represent empty positions).
  2. Sort the removed letters alphabetically. In this example, the removed letters are "lphabe", and they are sorted to become "abehlp".
  3. If any removed letters still exist, take the first one and move it to the leftmost empty position in the string. In this case, the string becomes "aa_____t", and the remaining removed letters are now "behlp".
  4. If any removed letters still exist, take the first one and move it to the rightmost empty position in the string. So, the string becomes "aa____bt", and the remaining removed letters are now "ehlp".
  5. Repeat steps 3 and 4 until no removed letters remain. In this example, the string becomes: "aae___bt" -> "aae__hbt" -> "aael_hbt" -> "aaelphbt".

You are given a String text containing a lowercase word. Return the scrambled word following the described procedure.

Constraints

  • text will contain between 2 and 50 characters, inclusive.
  • text will contain only lowercase letters ('a'-'z').
Examples
0)
"alphabet"
Returns: "aaelphbt"

Example from the problem statement.

1)
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"
Returns: "aabcdefghijklmnopqrstuvwyzxwvutsrqponmlkjihgfedcbx"
2)
"aa"
Returns: "aa"
3)
"abdfheca"
Returns: "abdfheca"
4)
"aaaaaaaaa"
Returns: "aaaaaaaaa"

Submissions are judged against all 72 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Scramble with a public method string scrambleWord(string text) · 72 test cases · 2 s / 256 MB per case

Submitting as anonymous