My understanding of the question


  • 1
    Q

    Actually, I think in this question, the point is that the t can be so large that it can't fit into memory one time. Thus, using indexOf might not work unless you chunk it. However, using two pointer should work since if t can't fit into memory we can simply use input stream to fix that.

    here is my solution, faster than other two pointer solution because s.chatAt() is expensive:

        public boolean isSubsequence(String s, String t) {
            if (s == null || t == null || s.length() > t.length()) {
                return false;
            }
            
            if (s.length() == 0) {
                return true;
            }
            
            int index = 0;
            char c = s.charAt(index);
            for (int i = 0; i < t.length(); i++) {
                if (t.charAt(i) == c) {
                    index++;
                    if (index == s.length()) {
                        return true;
                    }
                    c = s.charAt(index);
                }    
            }
        
            return false;
        }
    
    

  • 0
    T

    @quincyhehe Good Point! Your solution stores s.charAt(index) so that it does not need to call s.charAt(index) to do the comparison every time.


Log in to reply
 

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