Complexity of Regex substitution

前端 未结 8 1278
傲寒
傲寒 2020-12-01 16:58

I didn\'t get the answer to this anywhere. What is the runtime complexity of a Regex match and substitution?

Edit: I work in python. But would like to know in genera

8条回答
  •  有刺的猬
    2020-12-01 17:13

    From a purely theoretical stance:

    The implementation I am familiar with would be to build a Deterministic Finite Automaton to recognize the regex. This is done in O(2^m), m being the size of the regex, using a standard algorithm. Once this is built, running a string through it is linear in the length of the string - O(n), n being string length. A replacement on a match found in the string should be constant time.

    So overall, I suppose O(2^m + n).

提交回复
热议问题