Is Subsequence - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Monday 17 August 2020

Is Subsequence

Is Subsequence Is Subsequence

Example 1:

Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"
Output: false

 

Constraints:

  • 0 <= s.length <= 100
  • 0 <= t.length <= 10^4
  • Both strings consists only of lowercase characters.


class Solution {
public:
    bool isSubsequence(string s, string t) {
        
        int n=s.size(),m=t.size();
        
        if(n>m)return false;
        if(n==0)return true;
        int i=0,j=0;
        
        while(i<n&&j<m)
        {
            if(s[i]==t[j])
            {
                ++i,++j;
            }else{
                ++j;
            }
            if(i==n)return true;
        }
        return false;
        
        
        
    }
};