4ms Java character counting


  • 0
    Q

    Borrow the idea from substring problems

    public class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length() != t.length()){
                return false;
            }
            int[] map = new int[256];
            char[] sCharArray = s.toCharArray();
            char[] tCharArray = t.toCharArray();
            for(int i = 0; i < sCharArray.length; ++i){
                map[sCharArray[i]]++;
            }
            for(int i = 0; i < tCharArray.length; ++i){
                if(--map[tCharArray[i]] < 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.