[Amazon] Two Strings Are Anagrams (Compare Strings)

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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值