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.

Anagram:Only change the arrangement of the word, but the variety and number of chars in the word are identical.

Solution 1: #include<algorithm> sort

 class Solution {
public:
bool isAnagram(string s, string t) { //runtime:76ms
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t;
}
};

Solution 2: count

 class Solution {
public:
bool isAnagram(string s, string t) { //runtime:12ms
vector<int> count(,);
for(int i=;i<s.size();i++)
count[s[i]-'a']++;
for(int i=;i<t.size();i++)
count[t[i]-'a']--;
for(int i=;i<;i++)
if(count[i]!=)
return false;
return true;
}
};

【LeetCode】242 - Valid Anagram的更多相关文章

  1. 【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 ...

  2. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

  3. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  5. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  8. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  9. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

随机推荐

  1. UITableView的使用及性能优化

    UITableView可谓是日常开发中最重要的控件之一,而使用UITableView最重要的在于性能优化.iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITa ...

  2. Eclipse中user library包管理

    1.整理jar 2.将整理出的jar包在Eclipse中分别设置为用户librarywindow -> preferences -> java -> build path -> ...

  3. 解决Hibernate Write operations are not allowed in read-only mode的方法

    错误信息: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed i ...

  4. dojo 一 require 路径问题

    dojo.baseUrl baseUrl用来存储dojo.js存放 的跟目录,例如dojo.js的路径是“/web/scripts/dojo-1.3/dojo/dojo.js”则baseUrl为“/w ...

  5. Json工具类 - JsonUtils.java

    Json工具类,提供Json与对象之间的转换. 源码如下:(点击下载 - JsonUtils.java . gson-2.2.4.jar ) import java.lang.reflect.Type ...

  6. poj-3255-Roadblocks-路径可重复次短路

    题目: Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7075 Accepted: 2629 Descri ...

  7. html5 移动端单页面布局

    序     移动端的web网页使用的是响应式设计,但一般我们看到的网站页面都是跳转刷新得到的,比如通过点击一个menu后进入到另一个页面 今天来说下是移动端的单页面的布局.单页面就是一切操作和布局都是 ...

  8. Unix/Linux 关机命令

    关机命令 AIX shutdown now Solaris init 5 Redhat shutdown now Centos shutdown now

  9. XML Schema使用技巧——unique

    XML Schema使用技巧——unique   XML Scheam允许指定某个元素或属性的值在一定得范围内是唯一的.为了指定元素或属性值的唯一性,可以使用<xs:unqiue>元素,使 ...

  10. Tuning 01 Overview of Oracle Performance Tuning

    永无止境的调优 service level agreements: 是一个量化的调优的指标. performance 只要满足业务OK就可以了, 没必要调的很多, 因为有得必有失, 一方面调的特别优化 ...