[JAVA] ++ for magazine & -- for ransomNote / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] chs = new int[26];
            for(char ch : magazine.toCharArray()){
                chs[ch - 'a']++;
            }
            
            for(char ch : ransomNote.toCharArray()){
                chs[ch - 'a']--;
                if(chs[ch - 'a'] < 0)
                    return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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