Implement the isSubsequenceText method that checks whether the smaller text appears as a subsequence inside the larger text.

The input contains two strings: small and large. Your task is to check whether small is a subsequence of large.

A subsequence keeps the original order of characters but may skip some characters. For example, abc is a subsequence of ahbgdc because a, b, and c appear in the same order.

Example 1

Input
small (string) = abc
large (string) = ahbgdc
Returns (boolean) true

Example 2

Input
small (string) = axc
large (string) = ahbgdc
Returns (boolean) false

Example 3

Input
small (string) = ace
large (string) = abcde
Returns (boolean) true

Use two pointers: one pointer for small and one pointer for large.

Move through large. When the current character matches the needed character from small, move the small pointer. If all characters of small are matched, return true.

Pseudocode:

function isSubsequenceText(small, large):
    i = 0
    for each character in large:
        if i < length(small) and character == small[i]:
            i++
        if i == length(small):
            return true
    return i == length(small)
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.