5 lines Java sol using stringbuilder, ez sol.


  • 0
    J
    public boolean canConstruct(String ransomNote, String magazine) {
            StringBuilder sb = new StringBuilder(magazine);
            for(char c:ransomNote.toCharArray()){
                if(sb.indexOf(String.valueOf(c)) == -1 || sb.toString().equals("")) return false;
                sb.deleteCharAt(sb.indexOf(String.valueOf(c)));
            }
            return true;
        }
    

Log in to reply
 

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