longestRepeatingSubstring function

String longestRepeatingSubstring(
  1. String s
)

Longest Repeating Substring: Returns the longest substring that appears at least twice.

Implementation

String longestRepeatingSubstring(String s) {
  int n = s.length;
  int left = 1, right = n, resLen = 0, resStart = 0;
  while (left <= right) {
    int len = (left + right) ~/ 2;
    final idx = _search(s, len);
    if (idx != -1) {
      resLen = len;
      resStart = idx;
      left = len + 1;
    } else {
      right = len - 1;
    }
  }
  return s.substring(resStart, resStart + resLen);
}