Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Return the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).
Example 1:
Input: text = "abcabcabc" Output: 3 Explanation: The 3 substrings are "abcabc", "bcabca" and "cabcab".
Example 2:
Input: text = "leetcodeleetcode" Output: 2 Explanation: The 2 substrings are "ee" and "leetcodeleetcode".
Constraints:
1 <= text.length <= 2000text has only lowercase English letters.Problem summary: Return the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Trie
"abcabcabc"
"leetcodeleetcode"
find-substring-with-given-hash-value)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1316: Distinct Echo Substrings
class Solution {
private long[] h;
private long[] p;
public int distinctEchoSubstrings(String text) {
int n = text.length();
int base = 131;
h = new long[n + 10];
p = new long[n + 10];
p[0] = 1;
for (int i = 0; i < n; ++i) {
int t = text.charAt(i) - 'a' + 1;
h[i + 1] = h[i] * base + t;
p[i + 1] = p[i] * base;
}
Set<Long> vis = new HashSet<>();
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; j += 2) {
int k = (i + j) >> 1;
long a = get(i + 1, k + 1);
long b = get(k + 2, j + 1);
if (a == b) {
vis.add(a);
}
}
}
return vis.size();
}
private long get(int i, int j) {
return h[j] - h[i - 1] * p[j - i + 1];
}
}
// Accepted solution for LeetCode #1316: Distinct Echo Substrings
func distinctEchoSubstrings(text string) int {
n := len(text)
base := 131
h := make([]int, n+10)
p := make([]int, n+10)
p[0] = 1
for i, c := range text {
t := int(c-'a') + 1
p[i+1] = p[i] * base
h[i+1] = h[i]*base + t
}
get := func(l, r int) int {
return h[r] - h[l-1]*p[r-l+1]
}
vis := map[int]bool{}
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j += 2 {
k := (i + j) >> 1
a, b := get(i+1, k+1), get(k+2, j+1)
if a == b {
vis[a] = true
}
}
}
return len(vis)
}
# Accepted solution for LeetCode #1316: Distinct Echo Substrings
class Solution:
def distinctEchoSubstrings(self, text: str) -> int:
def get(l, r):
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
n = len(text)
base = 131
mod = int(1e9) + 7
h = [0] * (n + 10)
p = [1] * (n + 10)
for i, c in enumerate(text):
t = ord(c) - ord('a') + 1
h[i + 1] = (h[i] * base) % mod + t
p[i + 1] = (p[i] * base) % mod
vis = set()
for i in range(n - 1):
for j in range(i + 1, n, 2):
k = (i + j) >> 1
a = get(i + 1, k + 1)
b = get(k + 2, j + 1)
if a == b:
vis.add(a)
return len(vis)
// Accepted solution for LeetCode #1316: Distinct Echo Substrings
use std::collections::HashSet;
const BASE: u64 = 131;
impl Solution {
#[allow(dead_code)]
pub fn distinct_echo_substrings(text: String) -> i32 {
let n = text.len();
let mut vis: HashSet<u64> = HashSet::new();
let mut base_vec: Vec<u64> = vec![1; n + 1];
let mut hash_vec: Vec<u64> = vec![0; n + 1];
// Initialize the base vector & hash vector
for i in 0..n {
let cur_char = ((text.chars().nth(i).unwrap() as u8) - ('a' as u8) + 1) as u64;
// Update base vector
base_vec[i + 1] = base_vec[i] * BASE;
// Update hash vector
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
}
// Traverse the text to find the result pair, using rolling hash
for i in 0..n - 1 {
for j in i + 1..n {
// Prevent overflow
let k = i + (j - i) / 2;
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
if left == right {
vis.insert(left);
}
}
}
vis.len() as i32
}
#[allow(dead_code)]
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
}
}
// Accepted solution for LeetCode #1316: Distinct Echo Substrings
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1316: Distinct Echo Substrings
// class Solution {
// private long[] h;
// private long[] p;
//
// public int distinctEchoSubstrings(String text) {
// int n = text.length();
// int base = 131;
// h = new long[n + 10];
// p = new long[n + 10];
// p[0] = 1;
// for (int i = 0; i < n; ++i) {
// int t = text.charAt(i) - 'a' + 1;
// h[i + 1] = h[i] * base + t;
// p[i + 1] = p[i] * base;
// }
// Set<Long> vis = new HashSet<>();
// for (int i = 0; i < n - 1; ++i) {
// for (int j = i + 1; j < n; j += 2) {
// int k = (i + j) >> 1;
// long a = get(i + 1, k + 1);
// long b = get(k + 2, j + 1);
// if (a == b) {
// vis.add(a);
// }
// }
// }
// return vis.size();
// }
//
// private long get(int i, int j) {
// return h[j] - h[i - 1] * p[j - i + 1];
// }
// }
Use this to step through a reusable interview workflow for this problem.
Store all N words in a hash set. Each insert/lookup hashes the entire word of length L, giving O(L) per operation. Prefix queries require checking every stored word against the prefix — O(N × L) per prefix search. Space is O(N × L) for storing all characters.
Each operation (insert, search, prefix) takes O(L) time where L is the word length — one node visited per character. Total space is bounded by the sum of all stored word lengths. Tries win over hash sets when you need prefix matching: O(L) prefix search vs. checking every stored word.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.