Check for subsequence | Practice | GeeksforGeeks

Given two strings A and B, find if A is a subsequence of B.
Example 1:
Input:
A = AXY 
B = YADXCP
Output: 0 
Explanation: A is not a subsequence of B
as 'Y' appears before 'A'.

 
Example 2:
Input:
A = gksrek
B = geeksforgeeks
Output: 1
Explanation: A is a subsequence of B.
 
Your Task:  
You dont need to read input or print anything. Complete the function isSubSequence() which takes A and B as input parameters and returns a boolean value denoting if A is a subsequence of B or not. 
 
Expected Time Complexity: O(N) where N is length of string B.
Expected Auxiliary Space: O(1)

Constraints:
1<= |A|,|B| <=104
 
0
rjpatil20011 week ago
bool isSubSequence(string A, string B)    {       // code here       int j=0;      for(int i =0;i<B.length();i++){          if(B[i] == A[j]){              j++;          }      }       if(j == A.length()) return true;       else false;   }
0
jayesh293 weeks ago
Very Easy Java Solution:-
    boolean isSubSequence(String A, String B){
        int j=0,m=A.length(),n=B.length();
        for(int i=0; i<n && j<m; i++){
            if(A.charAt(j)==B.charAt(i)) j++;
        }
        return (j==m);
    }
0
dineshvishnukanth1 month ago
Python Solution:
 
class Solution:   def isSubSequence(self, A, B):       c=0       B=[i for i in B]       for i in range(len(A)):           for j in range(len(B)):               if A[i]==B[j]:                                      B[:j+1]=' '                   c+=1                                     break       if c==len(A):                       return(True)       else:           return(False)
0
geeky_kuldeep1 month ago
JS Solution using queueclass Solution {   isSubSequence(A,B){       let AQ = A.split('');      for (let b of B) {          if (b === AQ[0]) AQ.shift();          if (AQ.length === 0) return 1;      }      return 0;   }}
+1
mayank20212 months ago
C++ 0.3/1.7bool isSubSequence(string A, string B)    {       if(A.length() > B.length()) return 0;       if(A == B) return 1;              int j=0;              for(int i=0; i<B.length(); i++)        {            if(A[j]==B[i])            {                if(j==A.length()-1) return 1;             j++;            }        }              return 0;                 }
+1
tarun2002ts0212 months ago
my dp approch 
 int m=B.size(),n=A.size();       vector<vector<bool>>dp(m+1,vector<bool>(n+1,-1));       for(int i=0;i<m+1;i++)       {           for(int j=0;j<n+1;j++)           {               if(j==0)               dp[i][j]= true;               else if(i==0 && j!=0)               dp[i][j]=false;               else if(A[j-1]==B[i-1])               dp[i][j]=dp[i-1][j-1]&&true;               else                dp[i][j]=dp[i-1][j];           }       }       return dp[m][n];
0
anindiangeek2 months ago
        int first{0},second{0};
        while(second<B.length())
            if(B[second++]==A[first]) first++;
        return (second==A.length());
0
prasadkandekar5552 months ago
C++ O(N) Solution :)
class Solution{
    public:
    bool isSubSequence(string a, string b) {
        int n = a.length(), m = b.length();
        int i = 0, j = 0;
        while(i < n && j < m) {
            if(a[i] == b[j]) i++;
            j++;
        }
        return i == n;
    }
};
0
ervishnu19942 months ago
class Solution{   boolean isSubSequence(String A, String B){      int i=0;       int n=A.length(),m=B.length();       for(int j=0;j<m&&n>i;j++){           if(A.charAt(i)==B.charAt(j)){               i++;           }       }       if(i>=n)return true;       return false;   }}
0
akkeshri140420013 months ago
bool isSubSequence(string A, string B)    {       // code here       int m=A.length();       int n=B.length();       int i=0,j=0;              while(i<n and j<m){           if(A[j]!=B[i]){               i++;           }           else if(A[j]==B[i]){               i++;               j++;           }                  }       if(j==m){           return true;       }       return false;          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.