I am often asked to write functions which take a certain type of parameter such as a String and manipulate it in some way that would be a lot easier if it were some other data structure. Can I convert a String to a character array and set the String to null while considering my function to take O(1) space or does copying all the elements make it O(N) even if I immediately delete the other copy? I use Strings as an example because they are immutable. In another case, I would try to delete data from one structure as i add it to the other.
I am just using String and char[] as examples here. In general, I want to know if copying my data reduces the space complexity of my algorithm even if I'm deleting the other copy.
Copying penalizes your space complexity because the original and the copy are both in memory at the same time (even if this is only briefly the case), so you need to have that extra memory available. An in-memory algorithm is able to execute even if you don't have any more memory available.