
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to check whether a string is subsequence of other in C++
Suppose we have two strings S and T. We have to check whether S is subsequence of T or not.
So, if the input is like S = "abc", T = "adbrcyxd", then the output will be True
To solve this, we will follow these steps −
if s is same as t, then −
return true
n := size of s, m := size of t
j := 0
for initialize i := 0, when i < n, update (increase i by 1), do −
if t[j] is same as s[i], then −
(increase j by 1)
if j is same as size of t, then −
return true
return false
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: bool solve(string t, string s) { if(s == t) return true; int n = s.size(); int m = t.size(); int j = 0; for(int i = 0; i < n; i++){ if(t[j] == s[i]) j++; if(j == t.size()) return true; } return false; } }; main(){ Solution ob; string S = "abc", T = "adbrcyxd"; cout << ob.solve(S, T); }
Input
"abc", "adbrcyxd"
Output
1
- Related Articles
- C# program to check whether a given string is Heterogram or not
- Check whether a string ends with some other string - JavaScript
- C++ program to check whether given string is bad or not
- Java Program to check whether one String is a rotation of another.
- Haskell Program To Check Whether The Input String Is A Palindrome
- Program to check whether one tree is subtree of other or not in Python
- Java Program to Check Whether the Given String is Pangram
- Golang Program to Check Whether the Given String is Pangram
- Java program to check whether a given string is Heterogram or not
- Python program to check whether a given string is Heterogram or not
- Swift Program to check whether a given string is Heterogram or not
- C++ Program to Check Whether Graph is DAG
- Program to check whether final string can be formed using other two strings or not in Python
- Python program to check whether the string is Symmetrical or Palindrome
- Python Program to Check Whether a String is a Palindrome or not Using Recursion

Advertisements