← Back to challenges

String Expansion

JavaScriptHardstringslanguage_fundamentalsinterviewlogicregex

Instructions

Create a function which takes a string txt and expands it as per following rules:

  1. The numeric values represent the occurrence of each letter preceding that numeric value.
stringExpansion("3M2u5b2a1s1h2i1r") ➞ "MMMuubbbbbaashiir"
  1. The first occurrence of a numeric value should be the number of times each character behind it is repeated, until the next numeric value appears.
stringExpansion("3Mat")➞ "MMMaaattt"      // correct

stringExpansion("3Mat") ➞ "MMMat"          // wrong
stringExpansion("3Mat") ➞ "MatMatMat"      // wrong
  1. If there are consecutive numeric characters, ignore them all except last one.
stringExpansion("3M123u42b12a") ➞ "MMMuuubbaa"
  1. If there are two consecutive alphabetic characters then the string will remain unchanged.
stringExpansion("airforce") ➞ "airforce"
  1. Empty strings should return an empty string.
stringExpansion("") ➞ ""

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.