[Amazon] Two Strings Are Anagrams (Compare Strings)
来源:互联网 发布:易语言打码源码 编辑:程序博客网 时间:2024/06/09 14:16
Write a method anagram(s,t)
to decide if two strings are anagrams or not.
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
Example
Given s = "abcd"
, t = "dcab"
, return true
.
Given s = "ab"
, t = "ab"
, return true
.
Given s = "ab"
, t = "ac"
, return false
.
Challenge
O(n) time, O(1) extra space
思路:两串字符串分别统计每个出现字符的个数,如果有个数不等的,就不是anagram,反之
public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean anagram(String s, String t) { Map<Character,Integer> mapS=new HashMap<>(); Map<Character,Integer> mapT=new HashMap<>(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); if(!mapS.containsKey(ch)){ mapS.put(ch,1); }else{ mapS.put(ch,mapS.get(ch)+1); } } for(int i=0;i<t.length();i++){ char ch=t.charAt(i); if(!mapT.containsKey(ch)){ mapT.put(ch,1); }else{ mapT.put(ch,mapT.get(ch)+1); } } for(int i=0;i<s.length();i++){ char ch=t.charAt(i); if(mapS.get(ch)!=mapT.get(ch)){ return false; } } return true; }}
方法二:统计一个字符串里每个字符出现的个数,在统计另一个字符串字符的时候,个数- -,如果哪个字符的的总数不为0就false,反之。
public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean anagram(String s, String t) { if(s.length()!=t.length()){ return false; } int[] count=new int[256]; for(int i=0;i<s.length();i++){ count[s.charAt(i)]++; } for(int i=0;i<t.length();i++){ count[t.charAt(i)]--; } for(int i=0;i<count.length;i++){ if(count[i]!=0){ return false; } } return true; }}
思路代码一样的55. Compare Strings :点击打开链接
public class Solution { /** * @param A : A string includes Upper Case letters * @param B : A string includes Upper Case letter * @return : if string A contains all of the characters in B return true else return false */ public boolean compareStrings(String A, String B) { int[] count=new int[26]; for(int i=0;i<A.length();i++){ count[A.charAt(i)-'A']++; } for(int i=0;i<B.length();i++){ count[B.charAt(i)-'A']--; } for(int i=0;i<count.length;i++){ if(count[i]<0){ return false; } } return true; }}
阅读全文
0 0
- [Amazon] Two Strings Are Anagrams (Compare Strings)
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams
- Two Strings Are Anagrams-LintCode
- #158 Two Strings Are Anagrams
- Lintcode158 Two Strings Are Anagrams solution 题解
- 【4】Decide if two strings are anagrams or not
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- Two Strings are Anagram
- [CrackCode] 1.4 Write a method to decide if two strings are anagrams or not
- 158.Two Strings Are Anagrams-两个字符串是变位词(容易题)
- Compare Strings
- Compare Strings
- Two Strings
- Anagrams,Permutations,Multiply Strings
- altera cyclone v IOE delay 设置
- Java内存屏障和可见性
- 八大排序算法总结
- hbase概念学习(来源找不着了,有细微修改)
- glide的使用
- [Amazon] Two Strings Are Anagrams (Compare Strings)
- Python 3从入门到精通15-多维列表
- JavaScript prototype 属性和 对象属性、类属性的区别
- 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出
- C#6.0中10大新特性的应用和总结
- USB+Hid
- Dome小总结
- HDU 2577 How to Type(dp+状态标记)
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 分组+树状数组