[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 only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

判断给定两个字符串是否为相同字母不同排列的单词。

最简单的办法就是调用stl的排序函数sort给两个字符串s和t排序,然后比较是否相等即可,复杂度为O(nlogn);

如果嫌弃复杂度太高,还可以采用另外一种办法求每个字符个数判相等(题目已经假设只有小写字母那么也就只有26个),比较是否相等,复杂度为O(n);

我想到用map记录,然后看map是否equals。

public boolean isAnagram(String s, String t) {
        if (s.length() != t.length())
            return false;
        if (s.length() == 0 && t.length() == 0)
            return true;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        Map<Character, Integer> map1 = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i)))
                map.put(s.charAt(i), 1);
            map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
        }
        for (int i = 0; i < t.length(); i++) {
            if (!map1.containsKey(t.charAt(i)))
                map1.put(t.charAt(i), 1);
            map1.put(t.charAt(i), map1.get(t.charAt(i)) + 1);
        }
        if (map.equals(map1))
            return true;
        return false;
    }

另外一种,先排序之后比较两字符数组是否相等。

public boolean isAnagram(String s, String t) {
        if (s.length() != t.length())
            return false;
        if (s.length() == 0 && t.length() == 0)
            return true;
        char[] ss = s.toCharArray();
        char[] tt = t.toCharArray();
        Arrays.sort(ss);
        Arrays.sort(tt);
        for (int i = 0; i < tt.length; i++) {
            if (ss[i] != tt[i])
                return false;
        }
        return true;
    }
时间: 2024-10-26 05:20:20

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

[LeetCode] 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

[LeetCode] 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]125.Valid Palindrome

[题目] Valid Palindrome  Total Accepted: 3479 Total Submissions: 16532My Submissions Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a pa

[LeetCode]36.Valid Sudoku

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. [题意] 假设一个数独是有效的,则它符合数独游戏 规则(点击打开

[LeetCode] 20.Valid Parentheses

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

[LeetCode]65.Valid Number

题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambiguo

leetcode 20 Valid Parentheses 括号匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

LeetCode 20 Valid Parentheses(有效的括号)

翻译 给定一个只包含'(', ')', '{', '}', '[' 和']'的字符串,判断这个输入的字符串是否是有效的. 括号必须在正确的形式下闭合,"()" 和"()[]{}" 是有效的,但是 "(]" 和"([)]" 则不是. 原文 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the

LeetCode 36 Valid Sudoku(有效数独)(*)

翻译 数独板被部分填充,空格部分用'.'来填充. 一个部分填充的数组是否有效只需要看其填充的部分即可. 原文 代码 这道题写了一会,错了--因为输入太懒搞了,就直接看了别人写的-- class Solution { public: int a[9]; bool isValidSudoku(vector<vector<char>>& board) { memset(a,0,sizeof(a)); for (int i=0,j=0,row=0,col=0;i<9;++j,