Remove Leading/duplicate/trailing spaces
" My dog is great " --> "My dog is great"
Solution: Fast slow pointers
Two cases:
i == " "
first space of string: dont copy
duplicate space: dont copy
first space after char: copy
i != " " copy
Time comp: 1.5 traversals max = O(N)
Space comp: new char[] = O(N)
Last updated
Was this helpful?