首页 > 学院 > 开发设计 > 正文

Lintcode: Two Strings Are Anagrams

2019-11-14 23:47:40
字体:
来源:转载
供稿:网友
Lintcode: Two Strings Are Anagrams
Write a method anagram(s,t) to decide if two strings are anagrams or not.ExampleGiven 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 };


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表