Input: " " Output: " " Expected: ""
The extra blank space is trimmed. I dont' think that it is correct
Tested more cases and find that inner whitespace are also condensed.
I believe that the test cases are absolutely wrong. As the person who gave the case does not understand how to efficiently do this question. I post my c like solution here.
def reverseWords(self, s): def reverse(arr, i, j): while i < j: arr[i], arr[j] = arr[j], arr[i] i += 1 j -= 1 if not s: return s arr = list(s.strip()) # acutally we don't even need to strip. # reverse each word in arr startword = -1 for i in range(len(arr)): if startword < 0 and arr[i].isalnum(): startword = i elif startword > 0 and not arr[i].isalnum(): reverse(arr, startword, i - 1) startword = -1 reverse(arr, 0, len(arr) -1) return ''.join(arr)
The keypoint is to convert the immutable string to char array. You can do it easily in any programming langnuage. return the converted string at the end. This should be the ``correct'' answer as expected.
since you show that
- you know string is immutable
- you know the efficient inplace string processing.