package com.jwetherell.algorithms.strings; /** * Rotation of the string is some cyclic transformation of that string. * More formally a string s = uv is said to be a rotation of t if t = vu. *
* http://en.wikipedia.org/wiki/String_(computer_science)#Rotations
*
* http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation
*
* http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation
*
* @Author Szymon Stankiewicz
* This function implements Duval's algorithm.
*
* http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation#Duval.27s_Lyndon_Factorization_Algorithm
* Complexity: O(n)
*
* @param text
* @return lexicographicall minimal rotation of text
*/
public static String getLexicographicallyMinimalRotation(String text) {
return bestRotation(text, false);
}
/**
* Finds lexicographically maximal string rotation.
* Lexicographically maximal string rotation is a rotation of a string possessing the
* highest lexicographical order of all such rotations.
* Finding the lexicographically maximal rotation is useful as a way of normalizing strings.
*
* This function implements Duval's algorithm.
* http://en.wikipedia.org/wiki/Lexicographically_minimal_string_rotation#Duval.27s_Lyndon_Factorization_Algorithm
*
* Complexity: O(n)
*
* @param text
* @return lexicographicall minimal rotation of text
*/
public static String getLexicographicallyMaximalRotation(String text) {
return bestRotation(text, true);
}
}