For example, given a str of “Stackoverflow is for every one” and remove of “aeiou”,
the function should transform str to “Stckvrflw s fr vry n“.
I have one char array of string: str[] and one char array of chars to be removed:remove[]
My Solution: Loop str[] looking for each in character in remove[]. Shift str[] one place left every-time. I am sure better hack are possible.
Shifting the entire string left one place will make this an O(n^2) algorithm effectively. You can do this in-place, in linear time:
I’m assuming here that these are null terminated. If this is not the case, then you have to pass in the lengths as well and modify the algorithm appropriately. In particular, you will not be able to use strchr. Just for completeness, here’s a version that works with char arrays (not null-terminated).
And finally, this is as close to O(n) as we are going to get, I guess. I’m assuming 8 bit chars here and building a look-up table so this should run in O(n) + O(m) where m is the length of the match string.