389. Find the Difference

class Solution {
    public char findTheDifference(String s, String t) {

        Map<Character, Integer> hash = new HashMap<>();

        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (!hash.containsKey(c)){
                hash.put(c, 1);
            }
            else{
                hash.put(c, hash.get(c) + 1);
            }
        }

        for (int i = 0; i < t.length(); i++){
            char c = t.charAt(i);
            if (hash.get(c) == null || hash.get(c) <= 0) return c;

            hash.put(c, hash.get(c) - 1);
        }

        return '0';
    }
}

383. Ransom Note

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) return false;

        Map<Character, Integer> hash = new HashMap<>();

        for (int i = 0; i < magazine.length(); i++){
            char c = magazine.charAt(i);
            if (!hash.containsKey(c)){
                hash.put(c, 1);
            }
            else{
                hash.put(c, hash.get(c) + 1);
            }
        }

        for (int i = 0; i < ransomNote.length(); i++){
            char c = ransomNote.charAt(i);
            if (hash.get(c) == null || hash.get(c) <= 0) return false;

            hash.put(c, hash.get(c) - 1);
        }

        return true;
    }
}

44 Wildcard Matching

class Solution {
    public boolean isMatch(String s, String p) {

        int n = s.length(), m = p.length();
        boolean[][] dp = new boolean[n + 1][m + 1];

        dp[0][0] = true;
        for (int j = 1; j <= m; j++){
            if (p.charAt(j - 1) == '*') dp[0][j] = dp[0][j - 1];
        }

        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                char c1 = s.charAt(i - 1);
                char c2 = p.charAt(j - 1);

                if (c1 == c2 || c2 == '?'){
                    dp[i][j] = dp[i - 1][j - 1];
                }
                else if (c2 == '*'){
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                }
                else{
                    dp[i][j] = false;
                }
            }
        }

        return dp[n][m];
    }
}

583 Delete Operation for Two Strings (use Longest Common Sequence)

memory search

class Solution {
    public int minDistance(String word1, String word2) {
        int[][] memo = new int[word1.length() + 1][word2.length() + 1];
        return word1.length() + word2.length() - 2 * lcs(word1, word2, word1.length(), word2.length(), memo);
    }

    private int lcs(String word1, String word2, int index1, int index2, int[][] memo){
        if (index1 == 0 || index2 == 0) return 0;
        if (memo[index1][index2] > 0){
            return memo[index1][index2];
        }

        if (word1.charAt(index1 - 1) == word2.charAt(index2 - 1)){
            memo[index1][index2] = 1 + lcs(word1, word2, index1 - 1, index2 - 1, memo);
        }
        else{
            memo[index1][index2] = Math.max(lcs(word1, word2, index1 - 1, index2, memo), lcs(word1, word2, index1, index2 - 1, memo));
        }

        return memo[index1][index2];
    }
}

dp

class Solution {
    public int minDistance(String word1, String word2) {
        int n = word1.length(), m = word2.length();
        int[][] dp = new int[n + 1][m + 1];

        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                if (word1.charAt(i - 1) == word2.charAt(j - 1)){
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                }
                else{
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        return n + m - 2 * dp[n][m];
    }
}

718 Maximum Length of Repeated Subarray (DP, Longest Common Substring)

class Solution {
    public int findLength(int[] A, int[] B) {
        if (A == null || B == null) return 0;

        int n = A.length, m = B.length;
        int[][] dp = new int[n + 1][m + 1];

        int ans = 0;
        for (int i = 0; i <= n; i++){
            for (int j = 0; j <= m; j++){
                if (i == 0 || j == 0){
                    dp[i][j] = 0;
                }
                else{
                    if (A[i - 1] == B[j - 1]){
                        dp[i][j] = 1 + dp[i - 1][j - 1];
                        ans = Math.max(ans, dp[i][j]);    
                    }
                }
            }
        }

        return ans;
    }
}

115 Distinct Subsequences

class Solution {
    public int numDistinct(String s, String t) {
        // dp[i][j] t[i] with s[j], how many distinct subsequences
        int[][] dp = new int[t.length() + 1][s.length() + 1];

        for (int j = 0; j <= s.length(); j++){
            dp[0][j] = 1;
        }

        for (int i = 1; i <= t.length(); i++){
            for (int j = 1; j <= s.length(); j++){
                // dp[i][j - 1] : discard j
                // if t[i] = s[j] : dp[i - 1][j - 1] use j
                // else cannot use j
                dp[i][j] = dp[i][j - 1] + (t.charAt(i - 1) == s.charAt(j - 1) ? dp[i - 1][j - 1] : 0);
            }
        }

        return dp[t.length()][s.length()];
    }
}

results matching ""

    No results matching ""