Lintcode: Two Strings Are Anagrams

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

Lintcode: Two Strings Are AnagramsWrite 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 };

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航