博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 242. Valid Anagram
阅读量:7239 次
发布时间:2019-06-29

本文共 3513 字,大约阅读时间需要 11 分钟。

https://leetcode.com/problems/valid-anagram/description/

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?

  • 字符串处理,频率统计题。
  • 第一种方法是对字符串按字母排序,然后比较是否相等,简洁明了。
  • 第二种方法是用hash table。此题指明只有小写字母,所以‘a'-'z'26个英文字母,做张表就可以了。
  • 注意判断方法有两种,一种是都统计完再判断,一种是对s做加法,对t做减法,同时做减法时如果判断有负数统计出现,说明t中出现了多余字母时s中没有的。
  • https://leetcode.com/problems/valid-anagram/solution/
1 // 2 //  main.cpp 3 //  LeetCode 4 // 5 //  Created by Hao on 2017/3/16. 6 //  Copyright © 2017年 Hao. All rights reserved. 7 // 8  9 #include 
10 #include
11 #include
12 using namespace std;13 14 class Solution {15 public:16 // STL sort17 bool isAnagram(string s, string t) {18 sort(s.begin(), s.end());19 sort(t.begin(), t.end());20 21 return s == t;22 }23 24 // Hash25 bool isAnagram2(string s, string t) {26 if (s.length() != t.length()) {27 return false;28 }29 30 vector
dict(26);31 32 for (auto ch : s) {33 ++ dict[ch - 'a'];34 }35 36 for (auto ch : t) {37 -- dict[ch - 'a'];38 }39 40 for (auto iter : dict) {41 if (iter != 0)42 return false;43 }44 45 return true;46 }47 48 bool isAnagram3(string s, string t) {49 if (s.length() != t.length()) {50 return false;51 }52 53 vector
dict(26);54 55 for (auto ch : s) {56 ++ dict[ch - 'a'];57 }58 59 for (auto ch : t) {60 -- dict[ch - 'a'];61 62 if (dict[ch - 'a'] < 0) // Extra characters in string t63 return false;64 }65 66 return true;67 }68 };69 70 int main(int argc, char* argv[])71 {72 Solution testSolution;73 int result = 1;74 75 result = result && testSolution.isAnagram("anagram", "nagaram");76 result = result && testSolution.isAnagram("program", "margorp");77 result = result && !testSolution.isAnagram("rat", "car");78 result = result && !testSolution.isAnagram("aaab", "aaa");79 result = result && testSolution.isAnagram("", "");80 81 result = result && testSolution.isAnagram2("anagram", "nagaram");82 result = result && testSolution.isAnagram2("program", "margorp");83 result = result && !testSolution.isAnagram2("rat", "car");84 result = result && !testSolution.isAnagram2("aaab", "aaa");85 result = result && testSolution.isAnagram2("", "");86 87 result = result && testSolution.isAnagram3("anagram", "nagaram");88 result = result && testSolution.isAnagram3("program", "margorp");89 result = result && !testSolution.isAnagram3("rat", "car");90 result = result && !testSolution.isAnagram3("aaab", "aaa");91 result = result && testSolution.isAnagram3("", "");92 93 if (result)94 cout << "ALl tests pass!" << endl;95 else96 cout << "Tests fail!" << endl;97 98 return 0;99 }
View Code

 

转载于:https://www.cnblogs.com/pegasus923/p/8366661.html

你可能感兴趣的文章
Spring Boot 项目中使用JSP
查看>>
property干嘛的
查看>>
在香港五星级酒店里‘赏’早餐
查看>>
[转载] New Concept English 1——Lesson 5 Nice to meet you
查看>>
Java笔试面试
查看>>
iOS开发中遇到的一些问题以及解决办法总结
查看>>
Zabbix
查看>>
Unity 3D:控制相机旋转、移动、缩放等功能
查看>>
CALayer的m34 - 三维透视效果
查看>>
hdu 6243,6247
查看>>
C#操作Excel文件
查看>>
uchome在IE6下不居中和发布按钮不显示的解决办法
查看>>
iOS IPv6兼容支持和IPv6审核被拒收集整理
查看>>
Linux Shell 教程
查看>>
【补充习题七】积分不等式及定积分性质
查看>>
任意进制转换简单理解
查看>>
Unity Game窗口中还原Scene窗口摄像机操作 强化版
查看>>
javascript实现九九乘法表
查看>>
Eclipse的WorkingSet使用(转载)
查看>>
缓存系列之五:通过codis3.2实现redis3.2.8集群的管理
查看>>