Java solution with O(n)


  • 0
    public class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            if(magazine == null) {
                return false;
            }
            if(ransomNote == null) {
                return true;
            }
            
            List<Character> list = new ArrayList<>();
            for(Character ch: magazine.toCharArray()) {
                list.add(ch);
            }
            
            for(Character ch : ransomNote.toCharArray()) {
               if(list.contains(ch)) {
                   list.remove(ch);
               } else {
                   return false;
               }
           }
           return true;
            
        }
    }
    

Log in to reply
 

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