242题——Valid Anagram (哈希表)

Valid Anagram

Total Accepted: 9718 Total Submissions: 27840My Submissions

Question Solution

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.

Hide Tags

Hash Table Sort

Hide Similar Problems

(M) Anagrams

思路1:

这题采用两个长度为26的数组来存储从a到z的每个字母出现的次数,相当于将两个字符串都遍历一遍

然后将数组遍历一遍比较两个数组是否完全一样

若一样则true

否则false

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 using namespace std;
 5
 6 bool isAnagram(string s, string t) {
 7     vector<int> temp1(26,0);
 8     vector<int> temp2(26,0);
 9     for(auto i=s.begin();i!=s.end();i++)
10         temp1[(*i)-‘a‘]++;
11     for(auto j=t.begin();j!=t.end();j++)
12         temp2[(*j)-‘a‘]++;
13
14     for(int i=0;i<26;i++)
15         if(temp1[i]!=temp2[i])
16             return false;
17     return true;
18 }
19
20 int main()
21 {
22     string s="anagram";
23     string t="nagaram";
24     cout<<isAnagram(s,t)<<endl;
25     system("pause");
26 }

这道题可以采用哈希表来做:

建立两个哈希表来存储:键为字符,值为个数

然后遍历哈希表,其实这种我做的明显比之前的方法要繁琐,而且非空间,还有要分别遍历两个哈希表。

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<map>
 5 using namespace std;
 6
 7 bool isAnagram(string s, string t) {
 8     map<char,int> temp1;
 9     map<char,int> temp2;
10     for(auto i=s.begin();i!=s.end();i++)
11         temp1[*i]++;
12     for(auto i=t.begin();i!=t.end();i++)
13         temp2[*i]++;
14
15     for(auto i=temp1.begin();i!=temp1.end();i++)
16         if(i->second!=temp2[i->first])
17             return false;
18
19     for(auto i=temp2.begin();i!=temp2.end();i++)
20         if(i->second!=temp1[i->first])
21             return false;
22
23     return true;
24 }
25
26 int main()
27 {
28     string s="anagram";
29     string t="nagaram";
30     cout<<isAnagram(s,t)<<endl;
31     system("pause");
32 }
时间: 2024-08-06 07:36:51

242题——Valid Anagram (哈希表)的相关文章

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/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", re

【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. 题意: 给2个字符串,判断他们是否相等,只是顺序不一样(专业术语叫:变位词). 思路:

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

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",

noip模拟赛 好元素 哈希表的第一题

这是一道关于 题2好元素 2s [问题描述] 小A一直认为,如果在一个由N个整数组成的数列{An}中,存在以下情况: Am+An+Ap = Ai (1 <= m, n, p < i <= N , m,n,p可以相同),那么Ai就是一个好元素. 现在小A有一个数列,请你计算数列中好元素的数目 [输入格式] 第一行只有一个正整数N,意义如上. 第二行包含N个整数,表示数列{An}. [输出格式] 输出一个整数,表示这个数列中好元素的个数. [输入样例] Sample1 2 1 3 Sampl

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)

242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 un

242. Valid Anagram(两个字符串包含的字符是否完全相同)

Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note:You may assume

【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