Consecutive Subsequence Validator ā Problem Statement & Solution Guide
Problem Description
Given a list of integers asteroidSizes and a target list targetSequence, determine if the targetSequence appears in the same order and is consecutive within the asteroidSizes list.
Examples
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
true
Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we can see that the target sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] appears in the same order and is consecutive in the asteroidSizes list. Therefore, the output is true.
Input
[1, 2, 3, 2, 3, 4, 2, 3, 4, 5]
Output
false
Explanation: Step-by-step: Given the input [1, 2, 3, 2, 3, 4, 2, 3, 4, 5], we can see that the target sequence [1, 2, 3, 2, 3, 4, 2, 3, 4, 5] does not appear in the same order and is not consecutive in the asteroidSizes list. Therefore, the output is false.
Constraints
- 1 <= asteroidSizes.length <= 10^5
- 1 <= targetSequence.length <= 100
Optimal Approach & Strategy
The optimal approach involves using a single pass through the asteroidSizes array and iterating over the targetSequence array to verify consecutive matches, resulting in a time complexity of O(n). This approach ensures efficiency even for large inputs. It utilizes a two-pointer technique to track progress.
Brute Force Approach
A brute-force approach would involve iterating over the asteroidSizes array and checking every possible subsequence to see if it matches the targetSequence, resulting in a time complexity of O(n²). This approach would be inefficient for large inputs. It would also require additional nested loops to verify consecutive matches.
Verified Code Solutions
function consecutiveSubsequenceValidator(asteroidSizes, targetSequence) {
let n = asteroidSizes.length;
let m = targetSequence.length;
for (let i = 0; i <= n - m; i++) {
let isValid = true;
for (let j = 0; j < m; j++) {
if (asteroidSizes[i + j] !== targetSequence[j]) {
isValid = false;
break;
}
}
if (isValid) return true;
}
return false;
}class Solution {
public boolean isConsecutiveSubsequence(int[] asteroidSizes, int[] targetSequence) {
int targetIndex = 0;
for (int size : asteroidSizes) {
if (size == targetSequence[targetIndex]) {
targetIndex++;
if (targetIndex == targetSequence.length) {
return true;
}
} else if (size < targetSequence[targetIndex]) {
return false;
}
}
return false;
}
}def isConsecutiveSubsequence(asteroidSizes, targetSequence):
targetIndex = 0
for size in asteroidSizes:
if size == targetSequence[targetIndex]:
targetIndex += 1
if targetIndex == len(targetSequence):
return True
elif size < targetSequence[targetIndex]:
return False
return Falsefunction consecutiveSubsequenceValidator(asteroidSizes, targetSequence) {
let n = asteroidSizes.length;
let m = targetSequence.length;
for (let i = 0; i <= n - m; i++) {
let isValid = true;
for (let j = 0; j < m; j++) {
if (asteroidSizes[i + j] !== targetSequence[j]) {
isValid = false;
break;
}
}
if (isValid) return true;
}
return false;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.