Given a string, find two identical subsequences with consecutive indexes C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Identifying identical subsequences within a string is a common problem in computer science, particularly in fields dealing with data compression, sequence analysis, and string matching algorithms. This article focuses on finding two identical subsequences within a string, ensuring that their indexes are consecutive in C++.
Problem Definition
Given a string `s`, the task is to identify two subsequences that are identical and whose indexes are consecutive.
Subsequence
A subsequence of a string is derived by deleting zero or more characters from the string, without changing the order of the remaining characters. For example, the string `"abc"` has subsequences such as `"a"`, `"b"`, `"c"`, `"ab"`, `"ac"`, `"bc"`, and `"abc"`.
Identical Subsequences with Consecutive Indexes
The challenge is not only to find any two identical subsequences but to ensure that these subsequences' indexes appear consecutively in the original string. For instance, for the string `"abcabc"`, the subsequences `"abc"` and `"abc"` are identical, and their indexes in the original string are consecutive.
Approach and Solution
The problem can be approached using a combination of dynamic programming and hashing, which efficiently checks for subsequences.
Dynamic Programming
Dynamic programming (DP) can be utilized to store the results of intermediate subsequences previously computed, avoiding redundant calculations. We define a DP table `dp[i][j]`:
- `dp[i][j]` will be true if there is a subsequence ending at index `i` that can be extended to a subsequence starting at index `j`.
Hashing
Hashing will assist in quickly checking if a subsequence has already been found. By using a hash set, previously encountered subsequences can be stored and checked for subsequent identical occurrences.
C++ Implementation
Here is a C++ implementation that combines the above techniques:
- Complexity: The time complexity is approximately where `n` is the length of the string. This is primarily due to the nested loop structure depending iteratively over potential lengths and positions in the string.
- Space Complexity: The space complexity is also for storing the potentially unique substrings.
- Edge Cases: Consider scenarios where the string is empty or very small. Ensure logic handles these efficiently.
- String Characteristics: For strings with fewer unique characters, hashing might lead to collisions. Consider alternate data structures for higher uniqueness.
Related reading
- Given a string of a million numbers, return all repeating 3 digit numbers
- Given a word, convert it into a palindrome with minimum addition of letters to it
- Given an array, can I find in On the longest range, whose endpoints are the greatest values in the range?
- Given an array, find out the next smaller element for each element
- Group the numbers C
- gRPC cpp async server vs sync server
- Given an array of 0 and 1, find minimum no. of swaps to bring all 1s together only adjacent swaps allowed
- Given an array of integers, find the first missing positive integer in linear time and constant space

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.