Repository: TheAlgorithms/C-Plus-Plus
File: strings/duval.cpp (C++)

Code before:
 * equivalence class of "abc" is {"abc", "bca", "cab"}. The lexicographically
 * smallest string in this class is "abc".
 *
 * Duval's algorithm works by iterating over the string and finding the
 * smallest rotation of the string that is a Lyndon word. This is done by
 * comparing the string with its suffixes and finding the smallest suffix that
 * is lexicographically smaller than the string. This suffix is then added to
 * the result and the process is repeated with the remaining string.
 * The algorithm has a time complexity of O(n) where n is the length of the
 * string.
 *
 * @note While Lyndon words are described in the context of strings,
 * Duval's algorithm can be used to find the lexicographically smallest cyclic
 * shift of any sequence of comparable elements.
 *
 * @author [Amine Ghoussaini](https://github.com/aminegh20)
*/

#include <array>    ///< for std::array
#include <cassert>  ///< for assert
#include <cstddef>  ///< for std::size_t
#include <deque>    ///< for std::deque
#include <iostream> ///< for std::cout and std::endl
#include <string>   ///< for std::string
#include <vector>   ///< for std::vector

namespace string {
/**
 * @brief Find the lexicographically smallest cyclic shift of a sequence.
 * @tparam T type of the sequence
 * @param s the sequence
 * @returns the 0-indexed position of the least cyclic shift of the sequence
 */
template <typename T>
size_t duval(const T& s) {
    size_t n = s.size();
    size_t i = 0, ans = 0;
    while (i < n) {
        ans = i;
        size_t j = i + 1, k = i;
        while (j < n + n && s[j % n] >= s[k % n]) {
            if (s[k % n] < s[j % n]) {
                k = i;
            } else {
                k++;
            }
            j++;
        }
        while (i <= k) {
            i += j - k;
        }

Review comment (suggestion):
```suggestion
/**
 * @brief string manipulation algorithms
 * @namespace
 */
```


Code after:
 * equivalence class of "abc" is {"abc", "bca", "cab"}. The lexicographically
 * smallest string in this class is "abc".
 *
 * Duval's algorithm works by iterating over the string and finding the
 * smallest rotation of the string that is a Lyndon word. This is done by
 * comparing the string with its suffixes and finding the smallest suffix that
 * is lexicographically smaller than the string. This suffix is then added to
 * the result and the process is repeated with the remaining string.
 * The algorithm has a time complexity of O(n) where n is the length of the
 * string.
 *
 * @note While Lyndon words are described in the context of strings,
 * Duval's algorithm can be used to find the lexicographically smallest cyclic
 * shift of any sequence of comparable elements.
 *
 * @author [Amine Ghoussaini](https://github.com/aminegh20)
*/

#include <array>    /// for std::array
#include <cassert>  /// for assert
#include <cstddef>  /// for std::size_t
#include <deque>    /// for std::deque
#include <iostream> /// for std::cout and std::endl
#include <string>   /// for std::string
#include <vector>   /// for std::vector

/**
 * @brief string manipulation algorithms
 * @namespace
 */
namespace string {
/**
 * @brief Find the lexicographically smallest cyclic shift of a sequence.
 * @tparam T type of the sequence
 * @param s the sequence
 * @returns the 0-indexed position of the least cyclic shift of the sequence
 */
template <typename T>
size_t duval(const T& s) {
    size_t n = s.size();
    size_t i = 0, ans = 0;
    while (i < n) {
        ans = i;
        size_t j = i + 1, k = i;
        while (j < (n + n) && s[j % n] >= s[k % n]) {
            if (s[k % n] < s[j % n]) {
                k = i;
            } else {
                k++;
            }
            j++;