Top Interview 150
The Is Subsequence problem is a great challenge that involves iterating through strings to determine if one is a subsequence of the other. Letβs break down LeetCode 392: Is Subsequence and explore both standard and optimized solutions.
π Problem Description
Given two strings s and t:
- Return
trueifsis a subsequence oft. - A subsequence of a string is formed by deleting some characters (or none) from the original string without changing the relative order of the remaining characters.
π‘ Examples
Example 1
Input: s = "abc", t = "ahbgdc"
Output: true
Explanation: "abc" is a subsequence of "ahbgdc".
Example 2
Input: s = "axc", t = "ahbgdc"
Output: false
Explanation: "axc" is not a subsequence of "ahbgdc".
π JavaScript Solution
Approach 1: Two Pointer Technique
Use two pointers to traverse both strings, checking if the characters of s appear in the same order in t.
Implementation
var isSubsequence = function(s, t) {
let i = 0;
let j = 0;
while (i < s.length && j < t.length) {
if (s[i] === t[j]) {
i++;
}
j++;
}
return i === s.length;
};
π How It Works
-
Pointers:
- Use
ito traversesandjto traverset.
- Use
-
Character Matching:
- If
s[i] === t[j], move both pointers. - Otherwise, only move
jto check the next character int.
- If
-
Check Completion:
- If
ireaches the end ofs, then all characters insare matched in order int.
- If
π Complexity Analysis
- Time Complexity: O(n+m), where
n=s.lengthandm=t.length.- Traverse
tat most once andsat most once.
- Traverse
- Space Complexity: O(1), as no additional data structures are used.
π Dry Run
Input: s = "abc", t = "ahbgdc"
Follow-Up: Efficient Handling for Multiple s Strings
If there are many incoming s strings to check against a fixed t, preprocess t into an index map for efficient lookup.
Approach 2: Preprocessing t for Faster Matching
Preprocessing: Build a map of character positions for t.
- This allows for binary search to quickly locate the next valid position for each character in
s.
Optimized Implementation
var isSubsequence = function(s, t) {
// Preprocess t into a map of character indices
const indexMap = new Map();
for (let i = 0; i < t.length; i++) {
if (!indexMap.has(t[i])) {
indexMap.set(t[i], []);
}
indexMap.get(t[i]).push(i);
}
let prevIndex = -1; // Previous matching index in t
for (let char of s) {
if (!indexMap.has(char)) return false; // Character not in t
const positions = indexMap.get(char);
const nextIndex = binarySearch(positions, prevIndex);
if (nextIndex === -1) return false; // No valid position found
prevIndex = nextIndex; // Update to the latest matching index
}
return true;
};
// Helper function: Binary search for the next valid index
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left < arr.length ? arr[left] : -1;
}
π Complexity Analysis
-
Preprocessing:
- Time Complexity: O(m), where
m=t.length, to build the index map. - Space Complexity: O(m), for storing character indices.
- Time Complexity: O(m), where
-
Query:
- Time Complexity:
O(nβ logm), wheren=s.lengthandm=t.length. Each character in s requires a binary search int.
- Time Complexity:
π Dry Run
Input: s = "abc", t = "ahbgdc"
Index Map:
{
a: [0],
h: [1],
b: [2],
g: [3],
d: [4],
c: [5]
}
Steps:
Find a β Next valid index is 0.
Find b β Next valid index is 2.
Find c β Next valid index is 5.
Output: true
β¨ Pro Tips for Interviews
-
Understand Use Cases:
- Two-pointer is ideal for single queries.
- Preprocessing is better for multiple queries.
-
Discuss Edge Cases:
- Empty
s("") β Alwaystrue. - Characters in
snot int("xyz", "abc") β Alwaysfalse.
- Empty
-
Explain Preprocessing:
- Highlight how the index map reduces redundant computations.
π Learn More
Check out the detailed explanation and code walkthrough on my Dev.to post:
π Valid Palindrome - JavaScript Solution
Which approach would you choose for your scenario? Letβs discuss! π

Top comments (1)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!