Calculates the new index we should go to if we fail a comparison :param pattern: :return:
(pattern: str)
| 43 | |
| 44 | |
| 45 | def get_failure_array(pattern: str) -> list[int]: |
| 46 | """ |
| 47 | Calculates the new index we should go to if we fail a comparison |
| 48 | :param pattern: |
| 49 | :return: |
| 50 | """ |
| 51 | failure = [0] |
| 52 | i = 0 |
| 53 | j = 1 |
| 54 | while j < len(pattern): |
| 55 | if pattern[i] == pattern[j]: |
| 56 | i += 1 |
| 57 | elif i > 0: |
| 58 | i = failure[i - 1] |
| 59 | continue |
| 60 | j += 1 |
| 61 | failure.append(i) |
| 62 | return failure |
| 63 | |
| 64 | |
| 65 | if __name__ == "__main__": |
no test coverage detected