I wrote the following a java by using a regular expression to find a repeated String in the String s
. Now I try to find the complexity of it, if any one know what is the complexity of it, please let me know.
String s = "ABCABCAAAABBBBCCAAABCGABCABC";
Pattern pattern = Pattern.compile("(?:([ABC])(?!\\1)([ABC])\\1\\2)+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.print("FOUND");
}
Regular expressions can/are implemented by finite state machines. So statemachine with some fixed number of states and a fixed maximum number (T) of transitions between these states.
For each character a transition is taken until it is rejected or accepted.
So you have O( String.length() * T) or O( String.length() ) as T is a constant.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.