[英]time complexity of regular expression in java
I wrote the following a java by using a regular expression to find a repeated String in the String s
. 我用正则表达式来查找字符串重复字符串写了下面一个java
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.
因此,状态机具有一些固定数量的状态以及这些状态之间的固定最大转移次数(T)。
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. 所以您有O(String.length()* T)或O(String.length()),因为T是一个常数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.