import re
for _ in range(int(input())):
s=input() #input alphanumeric string
print(sum(map(int,re.findall('\d+',s))))

- 11,891
- 53
- 45
- 80

- 23
- 5
3 Answers
This should run in a linear time O(n). Check this questions related to the time complexities of regular expressions

- 139
- 1
- 10
-
Fine, but.. OP asked specifically about time complexity of regex implementation _in Python_, and complexity may differ from implementation to implementation. – Reinderien May 10 '22 at 01:35
The time complexity of findall for this pattern should be O(len(s)) The regex is not backtracking so it should be match-able in linear time. However I do not know the implementation of re, but I would be surprised if it would not match in linear time.

- 1,024
- 8
- 11
The time complexity is hard to mention as most of the regular expressions are formed on regular languages. Now all the regular languages have their Finite Automata which can either be represented by DFA, NFA or e-NFA. How much time a regex would take would no only be dependent on the size of the pattern to match but also the Finite Automata to compute that regex. It may also be dependent on the actual engine. If it directly constructs the DFA, then each time matching on a input string, is upper bounded by in length(O(n)).

- 2,289
- 1
- 14
- 38