• + 0 comments

    Java Solution:

    public static String abbreviation(String a, String b) {
            char[] s1 = a.trim().toCharArray();
            char[] s2 = b.toUpperCase().trim().toCharArray();
            int rows = s2.length;
            int cols = s1.length;
            
            boolean[][] dp = new boolean[rows+1][cols+1];
            dp[0][0] = true;
            //Fill the first col
            for(int row = 1; row<=rows; row++){
                dp[row][0] = false;
            }
            
            //Fill the first row
            for(int col = 1; col <= cols; col++){
                if(Character.isLowerCase(s1[col-1]))
                    dp[0][col] = dp[0][col-1];
                else
                    dp[0][col] = false;
            }
            
            for(int row = 1; row <= rows; row++){
                for(int col = 1; col <= cols; col++){
                    if(Character.toUpperCase(s1[col - 1]) == s2[row-1]){
                        if(Character.isLowerCase(s1[col-1])){
                            dp[row][col] = dp[row-1][col-1]?dp[row-1][col-1]: dp[row][col-1];
                        }else
                            dp[row][col] = dp[row-1][col-1];                       
                    }
                    else if(Character.isLowerCase(s1[col-1]))
                        dp[row][col] = dp[row][col-1];
                    else
                        dp[row][col] = false;
                }
            }
            return dp[rows][cols]? "YES" : "NO";
        }