Repository navigation
Expand file tree
/
Copy pathLyndonFactorization.java
More file actions
61 lines (52 loc) · 1.74 KB
/
Copy pathLyndonFactorization.java
File metadata and controls
61 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.thealgorithms.strings;
import java.util.ArrayList;
import java.util.List;
/**
* Factorizes a string into its Lyndon words using Duval's algorithm.
*
* <p>Duval's algorithm runs in O(n) time and uses O(1) auxiliary working
* space, excluding the returned list and its strings.
*
* <p>Reference:
* https://cp-algorithms.com/string/lyndon_factorization.html
*/
public final class LyndonFactorization {
private LyndonFactorization() {
}
/**
* Returns the standard Lyndon factorization of {@code text}.
*
* <p>The returned factors are in non-increasing lexicographic order
* according to Java's UTF-16 code-unit ordering.
*
* @param text input string to factorize
* @return Lyndon factors in non-increasing lexicographic order
* @throws IllegalArgumentException if {@code text} is null
*/
public static List<String> factorize(String text) {
if (text == null) {
throw new IllegalArgumentException("text must not be null");
}
List<String> factors = new ArrayList<>();
int n = text.length();
int start = 0;
while (start < n) {
int candidate = start + 1;
int match = start;
while (candidate < n && text.charAt(match) <= text.charAt(candidate)) {
if (text.charAt(match) < text.charAt(candidate)) {
match = start;
} else {
match++;
}
candidate++;
}
int length = candidate - match;
while (start <= match) {
factors.add(text.substring(start, start + length));
start += length;
}
}
return factors;
}
}