题目

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.

分析

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

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

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

AC代码

class Solution {
public:
//方法一:排序判断
bool isAnagram1(string s, string t) {
if (s.empty() && t.empty())
return true;
else if (s.empty() || t.empty())
return false; sort(s.begin(), s.end());
sort(t.begin(), t.end()); if (s == t)
return true;
return false;
}
//方法二:字母个数判相等
bool isAnagram(string s, string t) {
vector<int> count(26, 0);
for (int i = 0; i < s.size(); i++)
count[s[i] - 'a'] ++;
for (int i = 0; i < t.size(); i++)
count[t[i] - 'a'] --;
for (int i = 0; i < 26; i++)
if (count[i] != 0)
return false;
return true;
}
};

GitHub测试程序源码

LeetCode(242)Valid Anagram的更多相关文章

  1. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  2. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  4. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  5. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  6. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  7. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. 安装Jaspersoft Studio

    下载位置:http://community.jaspersoft.com/project/jaspersoft-studio/releases.

  2. D. Statistics of Recompressing Videos

    D. Statistics of Recompressing Videos time limit per test 3 seconds memory limit per test 256 megaby ...

  3. html5响应式

    (function (doc, win) { var docEl = doc.documentElement, resizeEvt = ‘orientationchange’ in window ? ...

  4. SpringBoot环境中使用MyBatis代码生成工具

    一.Maven配置文件中添加如下依赖 <dependency> <groupId>org.mybatis.generator</groupId> <artif ...

  5. SpringBoot 2.x (13):整合ActiveMQ

    ActiveMQ5.x不多做介绍了,主要是SpringBoot的整合 特点: 1)支持来自Java,C,C ++,C#,Ruby,Perl,Python,PHP的各种跨语言客户端和协议 2)支持许多高 ...

  6. Tomcat一

    Tomcat是如何处理http请求的 Tomcat有什么用? Tomcat是一个应用服务器,也是一个Servlet容器,用来接收前端传过来的请求,并将请求传给Servlet,并将Servlet的响应返 ...

  7. vuejs 学习旅程一

    来上海快一年了,一直在东钿金融工作着,这一年来主要负责公司前期的房产评估微信平台,公司IT部也是刚刚成立,成立IT部门不仅仅只是维护房产评估微信,而是要做一个互金理财平台.于是我一年来的主要工作是负责 ...

  8. html 之table标签结构学习

    一.HTML table标签结构 html 中table标签的结构情况,如下所示: <!-- table标签结构如下: <table> <thead> # thead表格 ...

  9. HDU 4734 F(x) (数位DP,基础)

    题意:  一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. ...

  10. BZOJ 4896 :[Thu Summer Camp2016]补退选 Trie树+Vector

    4896: [Thu Summer Camp2016]补退选 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 315  Solved: 97[Submi ...