Skip to content

Instantly share code, notes, and snippets.

@phlippieb
Last active November 19, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlippieb/11c83cd5e7802860144d8f7a033d1377 to your computer and use it in GitHub Desktop.
Save phlippieb/11c83cd5e7802860144d8f7a033d1377 to your computer and use it in GitHub Desktop.

This naive algorithm compares two strings, needle and haystack. It returns true if haystack "fuzzily" contains needle, which is when (almost) every character in needle is found in (almost) the same order, but not necessarily contiguously, in haystack. Up to allowed_errors characters in needle are allowed to not be found in haystack. Characters may also be swapped.

The following is a python implementation:

def stringFuzzilyContains(needle, haystack):
    # allow this many characters to not be found
    allowed_errors = 0
    
    # the last index where a character was found;
    # will begin search for next character only up to 1 position before this.
    last_found_index = 0
    
    # prevent reverse-searching by only stepping back once at a time
    did_step_back = False
    
    for c1 in needle:
        found_c1 = False
        for index in range(last_found_index - 1, len(haystack)):
            if index >= 0 and c1 == haystack[index] and not did_step_back:
                found_c1 = True
                did_step_back = index == last_found_index - 1
                last_found_index = index
                break
        if not found_c1:
            if allowed_errors > 0:
                allowed_errors -= 1
            else:
                return False
    return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment