Share my Java DP solution


  • 0
    D
    public boolean isSubsequence(String s, String t) {
            if (s == null || s.equals("")) return true;
            if (s != null && t == null) return false;
            int m = s.length(), n = t.length();
            boolean[][] dp = new boolean[2][n + 1];
            for (int i = 0; i <= n; i++)
                dp[0][i] = true;
                
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (s.charAt(i) == t.charAt(j))
                        dp[1][j + 1] = dp[0][j];
                    else
                        dp[1][j + 1] = dp[1][j];
                }
                for (int k = 0; k < n; k++)
                    dp[0][k] = dp[1][k];
            }
            return dp[1][n];
        }
    

Log in to reply
 

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