Simply your code by Java Lambdas and Stream APIs


  • 1
    X
    final int[] cnt = new int[26];
    s.chars().forEach(e -> cnt[e - 'a']++);
    return t.chars().filter(e -> --cnt[e - 'a'] < 0).limit(1).count() == 0;
    

    A more general implemenation by Multiset

    if (s == null || t == null) {
        return s == t;
    } else if (s.length() != t.length()) {
        return false;
    }
    
    final Multiset<Integer> multiset = new Multiset<>();
    s.chars().forEach(ch -> multiset.add(ch));
    return t.chars().filter(e -> multiset.removeAndGet(e) < 0).limit(1).count() == 0;
    

  • 0
    E

    Cool though a little bit obfuscated


Log in to reply
 

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