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 string contains only lowercase alphabets.

解法一:排序后判相等

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

解法二:计数判相等

class Solution {
public:
bool isAnagram(string s, string t) {
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 (2 solutions)的更多相关文章

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

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

  2. 【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 = &q ...

  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. 把linux文件夹压缩成tar.gz的命令

    解压 tar zxvf 文件名.tar.gz 压缩 tar zcvf software.tar.gz /usr/local/software

  2. NodeJS学习:搭建私有NPM

    工具 verdaccio nrm pm2 特点 verdaccio 的特点: 不同步拉取npm库,占据大量硬盘,没有硬盘被撑爆的问题: 安装配置极其简单,不需要数据库: 支持配置上游registry配 ...

  3. python学习之for循环

    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 实例: #!/usr/bin/env python for letter in 'Python': # 第一个实例 prin ...

  4. python全栈开发day17-常用模块collections,random,time,os,sys,序列化(json pickle shelve)

    1.昨日内容回顾 1.正则表达式     # 正则表达式 —— str           # 检测字符串是否符合要求     # 从大段的文字中找到符合要求的内容 1).元字符 #. # 匹配除换行 ...

  5. 通过工具SecureCRTPortable将项目部署到服务器上

    1.将项目打包 2.打开工具连接指定的ip 下面是一些命令 tab键可以有一些提示功能 ls 查看服务器当前目录 lls 查看硬盘当前目录 其实就是linux系统命令 ,服务器是正常命令  ,操作本电 ...

  6. 057 Hive项目案例过程

    1.说明 这里只粘贴一张,图,主要针对的hive的项目的实践过程. 2.图 3.需求 统计PV 统计注册人数 => 这个是一个公司会关注的,每天的注册率. 统计ip 统计跳出率 => ip ...

  7. Visual Studio 代码补全功能有时候会失效的原因

    大部分时候失效是因为你的代码有的地方有错误!!

  8. Ucinet6 + Netdraw 根据excel文件绘制网络拓扑图

    条件: 具备Ucinet6 和 Netdraw 两款软件的Windows excel文件格式(.xlsx  .xls  .csv):必须是数字,如果现有的文件不是数字,可以采用某种编码的方式将其映射成 ...

  9. 《Gradle权威指南》--自定义Android Gradle工程

    No1: minSdkVersion public void minSdkVersion(int minSdkVersion){ setMinSdkVersion(minSdkVersion); } ...

  10. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...