Simple Java Solution 2ms


  • 0
    A

    The idea is using string.indexOf( char s, int index) to check the position of the character. Replace the searching index to reduce searching time.

    class Solution {
        public boolean isSubsequence(String s, String t) {
            int prev = 0;
            int cur = 0;
            for(int i = 0; i<s.length(); i++){
                cur = t.indexOf( s.charAt(i) , prev);
                if(cur >= prev)
                    prev = cur+1;
                else
                    return false;
            }
            return true;
        }
    }
    

  • 1
    Y

    66666666666666


Log in to reply
 

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