Lintcode: Two Strings Are Anagrams

Write a method anagram(s,t) to decide if two strings are anagrams or not.

Example
Given s="abcd", t="dcab", return true

O(N)time, O(1) space

 1 public class Solution {
 2     /**
 3      * @param s: The first string
 4      * @param b: The second string
 5      * @return true or false
 6      */
 7     public boolean anagram(String s, String t) {
 8         // write your code here
 9         if (s==null || t==null || s.length()!=t.length()) return false;
10         int[] ss = new int[256];
11         int[] tt = new int[256];
12         for (int i=0; i<s.length(); i++) {
13             ss[s.charAt(i)]++;
14             tt[t.charAt(i)]++;
15         }
16         for (int j=0; j<256; j++) {
17             if (ss[j] != tt[j]) return false;
18         }
19         return true;
20     }
21 };
原文地址:https://www.cnblogs.com/EdwardLiu/p/4427467.html