[LeetCode] NO. 242 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.

[题目解析] 首先明确题意,需要判断组成两个字符串的所有字符是否完全相同,即组成的字符和对应个数完全一样。马上我们能想到用map来解决,key是character,value是该字符对应个数。根据其中一个字符串可以构造一个map,那么第二个字符串中构造的map应该完全一样,于是可以进行相关判断。

 public boolean isAnagram(String s, String t) {
        if(s.length() != t.length()) return false;
        HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        for(int index = 0; index < s.length(); index++){
            char ele = s.charAt(index);
            if(map.containsKey(ele)){
                map.put(ele, map.get(ele)+1);
            }else{
                map.put(ele,1);
            }
        }
        for(int index = 0; index < t.length(); index++){
            char ele = t.charAt(index);
            if(map.containsKey(ele)){
                map.put(ele, map.get(ele)-1);
                if(map.get(ele) < 0){
                    return false;
                }
            }else{
                return false;
            }
        }
        return true;
}

我们可以继续思考,有没有更方便的方法?另外一种方法是,对字符串按照字符进行排序,排序后的字符串应该完全相同。

    public boolean isAnagram(String s, String t) {
        char[] sarray = s.toCharArray();
        char[] tarray = t.toCharArray();
        Arrays.sort(sarray);
        Arrays.sort(tarray);
        return new String(sarray).equals(new String(tarray));
    }
时间: 2024-10-10 01:46:11

[LeetCode] NO. 242 Valid Anagram的相关文章

【LeetCode】242. Valid Anagram (2 solutions)

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. Note:You may assume the strin

【LeetCode】242. 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. Note:You may assume the string contains

【LeetCode】242. 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. Note:You may assume the string contains

242. Valid Anagram(C++)

242. 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. 题目大意: 判断两字符串含有的元素是否相同.

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 Difficulty: Easy 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",

[LeetCode]: 242: 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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜. 例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false 备注: 你可以假设字符串只包含小写字母. 跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗? 原文 Given two strings s and t, write a function to

Leetcode 242 Valid Anagram pytyhon

题目: 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. 用到了python的sorted函数,原型:sorted(data, cmp=

[LeetCode] 242. Valid Anagram Java

题目: 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. 题意及分析:要求判断两个字符串是否由相同的字符组成.这里有两种方法,(1)统计