Ad
  • Default User Avatar

    The filter should be first to speed the iteration of the map

  • Default User Avatar

    That isn't true, it works in that case because replace replaces all instances of the word on each call, but the foreach loop has the unmodified members of the array created using split.

    The real issue is if there are words that also contain their reversals inside of the array. For example,
    "12345 54321 12345"
    produces
    "54321 54321 54321"
    because of how replace searches the entire string for all instances of the CharSequence to change, while the foreach loop will have the old data.

  • Default User Avatar

    This solution can benefit from the n /= 10 trick as well as having a wrap-around index for your remainders array