Longest common substring problem suffix array part 2
Key Takeaways
The video demonstrates how to solve the longest common substring problem using a suffix array, with a focus on a full example with four strings and a minimum of two strings sharing the longest common substring. The solution involves building a suffix array and an LCP array, and then using a window-based approach to find the longest common substring.
Full Transcript
welcome back we're going to finish where we left off in the last video in this video I want to do a full example solving the longest common substring problem with a suffix array for this example we're going to have four strings s1 s2 s3 and s4 I have also selected the value of K to be equal to two meaning that we want a minimum of two strings of our pool of four to share the longest common substring between them I have also provided you with a concatenated text we'll be working with as well as the solution at the bottom of the screen in case you want to pause the video and figure it out for yourself the first step in finding the longest common substring between a set of our four strings is to build the suffix array and the LCP array which I have displayed on the right side and the left side respectively while I will be conducting the longest common substring algorithm notice the variables on the left as they change the window LCP and the window LCS values will track the longest common prefix and the longest common substring values for the current window and the LCS length and the Alsea setter will track the best values so far so let's get started initially our window starts at the top and we want the window to contain two different colors so our rule is to expand downwards when we do not meet this criteria as I expand down the suffix is green so we still only have one color I expand down again and still one more green suffix I expand downwards again and now we arrive at a blue suffix and here we are able to perform a range query for our window however our query isn't fruitful because the window longest common prefix value is zero so there's no longest common substring here when we sass by the window color criteria like we do now we decrease the window size I decrease the window size by one and still nothing interesting and decrease the window size again and this time look what we found the current window contains a longest common prefix length of two so we obtained the longest common substring BC and add it to our solution set now we keep shrinking the interval size because we meet the color requirement our window size is now too small because K is 2 so we need two different color strings so we expand once more now something interesting has happened because we find an LC p value of 3 which is larger than our current best value so we update the solution set to have the string and BCD instead of just BC which is one character longer now get to shrink the window size the window was now too small so we expand the LC p value of 0 here so that is no good so shrink the window size now expand to meet the color requirement we get an LC p value of 1 but that doesn't beat our current best which is 3 so shrink the window now we need to meet a color requirement so expand we have only blue string so we keep expanding an LC p value of 1 for this window range so that's no good so shrink now we have an LC p value of 2 we're getting closer to our best but still not good enough so we have to shrink and like let go now expand now something interesting is going on here we have a window LCP value of 3 which is equal to our best so far so instead of saying that the CDE our newfound longest common substring value beats BCD which is of the same length we keep both in the solution set alright so now let's shrink our window interval because we meet the color requirement now expand it we still need one more color expand again our LCP window value is zero so shrink and shrink again now expand LCP value of 1 here that's not good enough so smaller expand i'll see p value of 2 okay we might be getting closer but we meet color requirement so shrink now expand to meet the color requirement these two strings have an Elsa p value is 0 shrink now expand now shrink now we've reached the end and found our solution to the longest common substring problem with 4 strings and a k value of 2 as I was doing the window expanding and shrinking I want you to notice that each time the window either expanded or shrank I only ever moved one of the endpoints downwards and they were always going downwards so we know that the number of windows has to be linear in proportion to the number of suffixes that we have and the number of suffixes that we have is the length of our text T so we come to the conclusion that there must be a linear amount of windows that we must consider which is really good because we want our time complexity be quick so that's for now thank you for watching I hope you learned something and I will catch you next time
Original Description
Related Videos:
Suffix array intro: https://www.youtube.com/watch?v=zqKlL3ZpTqs
Longest common prefix (LCP) array: https://www.youtube.com/watch?v=53VIWj8ksyI
Counting unique substrings: https://www.youtube.com/watch?v=m2lZRmMjebw
Longest common substring 1/2: https://www.youtube.com/watch?v=Ic80xQFWevc
Longest common substring 2/2: https://www.youtube.com/watch?v=DTLjHSToxmo
Longest repeated substring: https://www.youtube.com/watch?v=OptoHwC3D-Y
Data structures repository:
https://github.com/williamfiset/algorithms
Kattis problem:
https://open.kattis.com/problems/lifeforms
My website:
http://www.williamfiset.com ===================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
🎓
Tutor Explanation
DeepCamp AI