C# O(n) solution


  • 0
    B
    public class Solution {
        public bool IsSubsequence(string s, string t) {
            if(s.Length == 0) { return true; }
            if(t.Length == 0) { return false; }
            
            int j = 0;
            
            for(int i = 0; i < t.Length; i++) {
                if(t[i] == s[j]) {
                    if(j == s.Length-1) {
                        return true;
                    }
                    j++;
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.