Java Solution


  • 0
    M
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            char[] r = ransomNote.replaceAll("\\s","").toCharArray(); // Ransom Char Array
            StringBuilder sb = new StringBuilder(magazine);
            for(char c : r){
                int index = sb.indexOf(String.valueOf(c));
                if(index == -1) return false;
                else{
                    sb.deleteCharAt(index);
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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