Is Subsequence Problem & Solution

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

See the is subsequence problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
  bool isSubsequence(string s, string t) {
    int j = 0;
    for (int i = 0; i < s.size(); ++i) {
      while (j < t.size() && s[i] != t[j]) {

      if (j == t.size()) {
        return false;
    return true;

Are you looking for a job?