I have an ArrayList of phrases containing "a", "b", "ab", "ab c", "ab cd".
The input might be "ab c" or "ab c". In either cases it should match with "ab c" from the ArrayList. I need an algorithm for it.
I have an ArrayList of phrases containing a , b , ab , ab
Share
I don’t know what language you’re using, but the simplest pseudocode is:
If you want it to be faster, try something like this:
This will go iterate through each string with unique indices, incrementing when either whitespace is found or a match. If the characters don’t match, the string is rejected and the outer loop is continued.
This pseudocode is untested, but should give you an idea of how to do it. I recommend going the remove whitespace route. It’s much simpler code, not too slow, and gives the reader very obvious clues as to what you’re trying to do.
In most languages, strings are immutable, so doing this replacement won’t affect the strings in the ArrayList.