One Pager Cheat Sheet

  • You can reverseOnlyAlphabetical characters in a given string with length <= 100000 and composed of ASCII characters in O(n) time and space complexity.
  • The two pointers with swap method is recommended to reverse a regular string.
  • We can reverseArray the alphabetical characters of a string using /[a-zA-Z]/ in a for loop to efficiently reverse only the alphabetical characters.
  • We can swap out alphabetical characters in the string with elements from our newly created reversedAlpha based on the position/index.
  • Using the .map and .filter methods, one can reverse only alphabetical characters in a more succinct manner.
  • We split the string into an array and loop over it with map to return non-alphanumeric characters in their current positions.
  • We splice and join our array to remove unmatched characters and turn it back into a string, resulting in an O(n) time and space complexity.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT

That's all we've got! Let's move on to the next tutorial.

If you had any problems with this tutorial, check out the main forum thread here.