[LeetCode] Valid Anagram

简介: Hash Table This idea uses a hash table to record the times of appearances of each letter in the two stringss and t.

 


Hash Table

This idea uses a hash table to record the times of appearances of each letter in the two stringss and t. For each letter in s, it increases the counter by 1 while for each letter in t, it decreases the counter by 1. Finally, all the counters will be 0 if they two are anagrams of each other.

The first implementation uses the built-in unordered_map and takes 36 ms.

 1 class Solution {
 2 public:
 3     bool isAnagram(string s, string t) {
 4         if (s.length() != t.length()) return false;
 5         int n = s.length();
 6         unordered_map<char, int> counts;
 7         for (int i = 0; i < n; i++) {
 8             counts[s[i]]++;
 9             counts[t[i]]--;
10         }
11         for (auto count : counts)
12             if (count.second) return false;
13         return true;
14     }
15 };

Since the problem statement says that "the string contains only lowercase alphabets", we can simply use an array to simulate the unordered_map and speed up the code. The following implementation takes 12 ms.

 1 class Solution {
 2 public:
 3     bool isAnagram(string s, string t) {
 4         if (s.length() != t.length()) return false;
 5         int n = s.length();
 6         int counts[26] = {0};
 7         for (int i = 0; i < n; i++) { 
 8             counts[s[i] - 'a']++;
 9             counts[t[i] - 'a']--;
10         }
11         for (int i = 0; i < 26; i++)
12             if (counts[i]) return false;
13         return true;
14     }
15 };

Sorting

For two anagrams, once they are sorted in a fixed order, they will become the same. This code is much shorter (this idea can be done in just 1 line using Python as here). However, it takes much longer time --- 76 ms in C++.

1 class Solution {
2 public:
3     bool isAnagram(string s, string t) {
4         sort(s.begin(), s.end());
5         sort(t.begin(), t.end());
6         return s == t; 
7     }
8 };

 

目录
相关文章
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
67 0
LeetCode 367. Valid Perfect Square
LeetCode 242. Valid Anagram
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
54 0
LeetCode 242. Valid Anagram
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
67 0
LeetCode 125. Valid Palindrome
|
算法
LeetCode 65. Valid Number
验证给定字符串是否可以解释为十进制数。
68 0
LeetCode 65. Valid Number
Leetcode-Easy 20. Valid Parentheses
Leetcode-Easy 20. Valid Parentheses
90 0
Leetcode-Easy 20. Valid Parentheses
LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 有效字符串需满足: 左括号必须用相同类型的右括号闭合。
741 0
|
Java
[LeetCode] Valid Parentheses 验证括号是否有效闭合
链接:https://leetcode.com/problems/valid-parentheses/#/description难度:Easy题目:20.
983 0
LeetCode 242 Valid Anagram(有效字谜)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50498370 翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜。
1062 0
|
人工智能 Java
LeetCode 36 Valid Sudoku(有效数独)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50118647 翻译 数独板被部分填充,空格部分用'.'来填充。
924 0
|
4天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0