(2019-04-15, 10:39 PM)Y_Less Wrote: Another way you can do this is to loop through once, and shift all letters backwards. ?Every time you see a space you increase this shift offset:
PHP Code:DeSpace(string[])
{
new
// Read index.
i = -1,
// Write index.
j = -1,
// Current character.
ch,
// Did we already see a space?
bool:space;
do
{
if ((ch = string[]) != ' ')
{
// Not a space, just copy it.
string[] = ch;
// Mark us as having NOT just seen a space.
space = false;
}
else if (!space)
{
// First space. ?Any more just ignore them.
string[] = ch;
// Mark us as having just seen a space.
space = true;
}
}
while (ch);
}
Is this better? ?I don't know. ?It will depend on the length of the string and the number of double spaces.
(2019-04-16, 11:21 AM)Y_Less Wrote: This is what I meant by it depending on how many spaces there are. ?My code always runs in O(n) - it doesn't matter how long the string is, nor how many duplicate spaces there are. ?"strdel" works by copying the rest of the string over the deleted part, which means it has to copy all the rest of the string every time there is a duplicated space. ?This makes its worst-case something like O(n^2).
hmm don't thought that?strdel so working, in this case?that's really nice, thanks!
![[Image: aWbv5wm.gif]](https://imgur.com/aWbv5wm.gif)