Grokking Google Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Is Subsequence
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given two strings s and t, return true if the string s is a subsequence of the string t. Otherwise, return false.

A subsequence of a string is a new string that can be formed from the original string by eliminating some (can be zero) of the characters without changing the order of the remaining characters.

Examples

  • Example 1:

    • Input: s = "hello", t = "xyhealoloo"
    • Expected Output: true
    • Justification: By removing "x", "y", "a", "o", and the second "o" from t, s can be obtained while maintaining the original order.
  • Example 2:

    • Input: s = "axc", t = "ahbgdc"
    • Expected Output: false
    • Justification: There is no way to remove characters from t to achieve s without altering the sequence of "axc".
  • Example 3:

    • Input: s = "abc", t = "aabc"
    • Expected Output: true
    • Justification: By removing the first "a" in t, s is formed while preserving the character sequence.

Solution

To solve this problem, we adopt a two-pointer technique that iteratively compares characters from s and t. One pointer is used to traverse s, and another pointer for t. The idea is to advance both pointers when characters match, indicating that part of the subsequence has been found. When characters do not match, only the pointer for t advances, searching for the next potential matching character.

This method is effective because it linearly scans through both strings simultaneously, ensuring that the relative order of characters in s is maintained in t. By the end of the traversal, if we have successfully navigated through all characters in s, it confirms that s is indeed a subsequence of t.

Step-by-Step Algorithm

  1. Initialize Pointers: Start by initializing two pointers, indexS and indexT, to 0. These pointers will be used to traverse through the strings s and t, respectively.

  2. Traverse Through t: Begin a loop that continues as long as indexT is less than the length of t and indexS is less than the length of s. This loop will go through each character of t to check against s.

  3. Check Character Match: Inside the loop, compare the current character of t (pointed to by indexT) with the current character of s (pointed to by indexS).

    • If the characters match, increment indexS by 1 to move to the next character in s. This step is crucial as it indicates that part of the subsequence has been found.
  4. Move to Next Character in t: After the check or in case of a mismatch, increment indexT by 1 to continue scanning through t.

  5. Check Subsequence Found: After exiting the loop, check if indexS equals the length of s. This condition signifies that all characters in s have been found in t in the correct order.

    • If indexS equals the length of s, return true, indicating s is a subsequence of t.
    • If the loop ends and indexS is not equal to the length of s, return false, indicating s is not a subsequence of t.

Algorithm Walkthrough

Let's consider the Input: s = "hello", t = "xyhealoloo"

  • Initial Setup: indexS = 0, indexT = 0.
  1. Step 1: Begin Loop
    • indexT = 0, t[0] = 'x', indexS = 0, s[0] = 'h'. They do not match, so only indexT is incremented.
  2. Step 2: Next Iteration
    • indexT = 1, t[1] = 'y', still no match. Increment indexT.
  3. Step 3: Finding 'h'
    • indexT = 2, t[2] = 'h', matches s[0] = 'h'. Increment both indexS and indexT.
  4. Step 4: Finding 'e'
    • Continue to indexT = 3, t[3] = 'e', matches s[1] = 'e'. Increment both indexS and indexT.
  5. Step 5: Skipping to 'l'
    • indexT increments through 'a', until it finds the first 'l' at indexT = 5. s[2] = 'l' matches, increment both.
  6. Step 6: Finding Second 'l'
    • indexT = 6, t[6] = 'o', no match, increment indexT.
    • indexT = 7, t[7] = 'l', matches s[3] = 'l'. Increment both.
  7. Step 7: Finding 'o'
    • indexT = 8, t[8] = 'o', matches s[4] = 'o'. Increment both.
  8. Step 8: Conclusion
    • Now, indexS = 5, which equals the length of s. This means every character in s has been found in t in order, confirming s is a subsequence of t.
Image

Code

Python3
Python3

. . . .

Complexity Analysis

Time Complexity

The time complexity of the algorithm is O(n), where (n) is the length of string t. This is because, in the worst case, we traverse the entire string t once, comparing each character with characters from s. The traversal and comparisons for each character occur in constant time.

Space Complexity

The space complexity is O(1). Only a fixed number of variables (indexS and indexT) are used, regardless of the input size. This makes the space complexity constant, as it does not scale with the size of the input strings.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible