Ad
  • Default User Avatar

    To enhance fastness, it would be better to convert the String to StringBuilder, do the replaces like we did with String, and then convert the SB back to String. But it would still remain in O(n), thus it may not be worth the effort, but is nice to know.

  • Default User Avatar

    Both solutions are in the same complexity class O(n). So it's not really a win to use a for loop, instead of just replacing several times. Your code has also a bad readability, and is therefore less maintainable in production.

    Our method will not be slow like you say, only somewhat slower than yours --> See big O notation: https://en.wikipedia.org/wiki/Big_O_notation

  • Custom User Avatar

    Yes, your solution is shorter. But I think that we (developers) should write faster code. Imagine that your method will execute 1_000_000 times in production every 1 minute. It will be slow.

    Also I think that this kata we can't solve with regex, because we have several replace templates.

    Sorry for bad English.

  • Custom User Avatar

    Correct, but its more concise than what you did with a boilerplate for loop. One line versus 11 lines on your code. Best way would of been to use regex and call .replace() once.

  • Custom User Avatar

    This will create 5 new String instances. Also this solution will make 5 loops for replace all characters.