Suffix arrays in Data Structures Theory - Time & Space Complexity
Suffix arrays help us quickly find patterns in text by sorting all endings of a string.
We want to know how the time needed to build and use suffix arrays grows as the text gets longer.
Analyze the time complexity of the following suffix array construction code.
function buildSuffixArray(text) {
let suffixes = [];
for (let i = 0; i < text.length; i++) {
suffixes.push({index: i, suffix: text.slice(i)});
}
suffixes.sort((a, b) => a.suffix.localeCompare(b.suffix));
return suffixes.map(item => item.index);
}
This code creates all suffixes of the text, sorts them alphabetically, and returns their starting positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and sorting all suffixes of the string.
- How many times: There are as many suffixes as the length of the text (n), and sorting compares these suffixes multiple times.
As the text length grows, the number of suffixes grows linearly, but sorting them involves comparing many characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons of suffixes, each up to length 10 |
| 100 | About 10,000 comparisons, each up to length 100 |
| 1000 | About 1,000,000 comparisons, each up to length 1000 |
Pattern observation: The work grows much faster than the input size because sorting compares many pairs of suffixes, each possibly long.
Time Complexity: O(n² log n)
This means the time needed grows roughly like the square of the text length times a logarithm, making it slower for very long texts.
[X] Wrong: "Sorting suffixes is just like sorting numbers, so it should be O(n log n)."
[OK] Correct: Each suffix is a string that can be very long, so comparing them takes extra time, making sorting much slower than simple number sorting.
Understanding suffix arrays and their time complexity shows you can analyze complex data structures and their costs, a valuable skill in many coding challenges.
"What if we used a more advanced algorithm that builds the suffix array in O(n) time? How would that change the time complexity?"
Practice
Solution
Step 1: Understand suffix array definition
A suffix array is a list of all suffixes (endings) of a text sorted alphabetically.Step 2: Compare options with definition
Only An array listing all suffixes of a text sorted alphabetically correctly describes this; others describe different concepts.Final Answer:
An array listing all suffixes of a text sorted alphabetically -> Option BQuick Check:
Suffix array = sorted suffixes list [OK]
- Confusing suffix array with prefix array
- Thinking it stores character frequencies
- Assuming it sorts words, not suffixes
banana?Solution
Step 1: List all suffixes of 'banana' with their start indices
Suffixes: 0:'banana', 1:'anana', 2:'nana', 3:'ana', 4:'na', 5:'a'Step 2: Sort suffixes alphabetically and note indices
Sorted suffixes: 'a'(5), 'ana'(3), 'anana'(1), 'banana'(0), 'na'(4), 'nana'(2)Final Answer:
[5, 3, 1, 0, 4, 2] -> Option AQuick Check:
Sorted suffix indices = [5,3,1,0,4,2] [OK]
- Sorting indices instead of suffix strings
- Confusing prefix indices with suffix indices
- Reversing the order of suffixes
[3, 0, 4, 1, 5, 2] for a string, what is the likely original string if the suffixes sorted alphabetically are ['ana', 'banana', 'na', 'anana', 'a', 'nana']?Solution
Step 1: Match suffixes to indices
Suffixes: 'ana'(3), 'banana'(0), 'na'(4), 'anana'(1), 'a'(5), 'nana'(2)Step 2: Reconstruct string from suffixes and indices
Suffix starting at 0 is 'banana', so original string is 'banana'Final Answer:
banana -> Option DQuick Check:
Suffix array matches 'banana' suffixes [OK]
- Assuming suffix array directly shows original string
- Mixing up suffix order with original string order
- Ignoring suffix start positions
"apple":
suffixes = [s[i:] for i in range(len(s))] suffixes.sort() suffix_array = [s.index(suf) for suf in suffixes]
Solution
Step 1: Understand s.index() behavior
s.index() returns the first occurrence index of substring, which can be wrong if suffix repeats.Step 2: Identify impact on suffix array
If suffixes repeat, s.index() returns same index multiple times, causing incorrect suffix array.Final Answer:
Using s.index() can return wrong indices if suffixes repeat -> Option AQuick Check:
Repeated suffixes break s.index() correctness [OK]
- Assuming s.index() always gives correct suffix start
- Not sorting suffixes before indexing
- Starting range incorrectly
"ana" in the text "banana" using its suffix array [5, 3, 1, 0, 4, 2]. Which approach correctly uses the suffix array for this search?Solution
Step 1: Understand suffix array usage for pattern search
Suffix arrays allow binary search on sorted suffixes to quickly find pattern matches.Step 2: Evaluate options
Perform a binary search on suffix array indices comparing suffixes with 'ana' to find matches correctly describes binary search on suffix array; others do not use suffix array properly.Final Answer:
Perform a binary search on suffix array indices comparing suffixes with 'ana' to find matches -> Option CQuick Check:
Binary search on suffix array = efficient pattern search [OK]
- Linear scan ignoring suffix array benefits
- Sorting pattern instead of suffixes
- Using suffix array for unrelated tasks
Ready to prove it?
Pass the test and this pattern moves unknown
