We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticStringabbreviation(Stringa,Stringb){char[]s1=a.trim().toCharArray();char[]s2=b.toUpperCase().trim().toCharArray();introws=s2.length;intcols=s1.length;boolean[][]dp=newboolean[rows+1][cols+1];dp[0][0]=true;//Fill the first colfor(introw=1;row<=rows;row++){dp[row][0]=false;}//Fill the first rowfor(intcol=1;col<=cols;col++){if(Character.isLowerCase(s1[col-1]))dp[0][col]=dp[0][col-1];elsedp[0][col]=false;}for(introw=1;row<=rows;row++){for(intcol=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];}elsedp[row][col]=dp[row-1][col-1];}elseif(Character.isLowerCase(s1[col-1]))dp[row][col]=dp[row][col-1];elsedp[row][col]=false;}}returndp[rows][cols]?"YES":"NO";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Abbreviation
You are viewing a single comment's thread. Return to all comments →
Java Solution: