Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

 

public class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.length()!=t.length()) return false;
        StringBuffer sb = new StringBuffer(t);
        for(int i=0;i<s.length();i++) {
            int index = sb.indexOf(s.charAt(i)+"");
            if(index==-1) {
                return false;
            }
            else {
                sb.deleteCharAt(index);
            }
        }
        return true;
    }
}

 

posted @ 2015-09-12 18:54  mrpod2g  阅读(95)  评论(0编辑  收藏  举报