Straight Java solution


  • 0
    P

    Hi folks,

    I am sure my solution is not optimized when comes to the scenario "If there are lots of incoming S, say S1, S2, ... , Sk ", I am also posting this hoping you guys can kindly share some thoughts to improve base my solution for the scenario mentioned above.

    """
    public boolean isSubsequence(String s, String t) {

        int s_len = s.length();
        int t_len = t.length();
        if(s_len == 0){
            return true;
        }
        int[] sol = new int[s_len + 1];
        sol[0] = 0; // with 0 characters, index of the last char is 0
        int index = 0;
        int temp = 0;
        boolean result = true;
        
        for(int i = 1; i <= s_len; i++){
            char c = s.charAt(i-1);
            
            temp = t.indexOf(c);
            if(temp == -1){
                result = false;
                break;
            }
            else{
                sol[i] = temp;
                t = t.substring(sol[i]+1);
            }
        }
        return result;
    }
    

    """

    other than that, I came up with the solution based on typical DP question solution pattern.


Log in to reply
 

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