题目:

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.

思路:

统计第一个字符串的每一个字符出现的次数,再统计第二个字符串的每一个字符出现的次数。然后比較,假设都同样则返回true。

假设不同返回false。

代码1:

class Solution {
public:bool isAnagram(string s, string t) {int len1 = s.size();int len2 = t.size();if(len1 != len2)return false;int map[26] = {0};//因为题目确定每一个字符都是小写字母。因此仅仅须要申请26个空间就可以for(int i = 0 ; i < len1 ; i++){map[s[i]-'a']++;}for(int i = 0 ; i < len1 ; i++){map[t[i]-'a']--;if(map[t[i]-'a'] < 0)return false;}return true;}
};

代码2:

class Solution {
public:bool isAnagram(string s, string t) {int len1 = s.size();int len2 = t.size();if(len1 != len2)return false;int map[26] = {0};//因为题目确定每一个字符都是小写字母,因此仅仅须要申请26个空间就可以for(int i = 0 ; i < len1 ; i++){map[s[i]-'a']++;map[t[i]-'a']--;}for(int i = 0 ; i < len1 ; i++){if(map[t[i]-'a'] != 0)return false;}return true;}
};

转载于:https://www.cnblogs.com/gavanwanggw/p/6878087.html

LeetCode OJ 之 Valid Anagram相关推荐

  1. LeetCode OJ:Valid Anagram(有效字谜问题)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  2. Leet Code OJ 242. Valid Anagram [Difficulty: Easy]

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

  3. Leetcode题目:Valid Anagram

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

  4. [LeetCode] NO. 242 Valid Anagram

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

  5. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  6. LeetCode 32. Longest Valid Parentheses

    问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...

  7. LeetCode算法入门- Valid Parentheses -day11

    LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...

  8. [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 ...

  9. [LeetCode] Valid Anagram - 字符串排序比较系列

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

最新文章

  1. Windows Server 2008壮烈牺牲
  2. 【ros】4.rosbag的相关用法
  3. api key for Alpha Vantage
  4. js父页面和子页面之间传值
  5. html5 梵高 星,梵高作品欣赏《星空》
  6. DCMTK:将DICOM文件的内容转换为JSON格式
  7. (十)java springboot b2b2c shop 多用户商城系统源码:服务网关zuul初级篇
  8. 我被喷了:这样写代码是多此一举?
  9. visualstudiopython使用方法,使用python解析VisualStudio .csproj文件的最佳方法
  10. div垂直,水平居中
  11. arcgis中img 批量转换tif_在ArcGIS中Python使用及批量裁剪示技巧讲解 !
  12. 查看exe代码_【安全风险通告】Windows Type 1字体解析远程代码执行漏洞安全风险通告...
  13. php的慢查询,慢查询是什么
  14. 测试用例的定义,测试用例模板,测试用例的作用,测试用例编写注意事项
  15. android实现图片自动轮播代码,Android实现图片轮播切换实例代码
  16. 在虚拟机上安装使用LoadRunner教程
  17. Debian修改IP地址
  18. 关于OSPF的区域划分规则
  19. 基于单片机的电机转速测量设计
  20. 强化学习15——Proximal Policy Optimization (PPO)算法详细推导

热门文章

  1. 如何更新mysql数据库字段_如何使用MySQL一个表中的字段更新另一个表中字段
  2. 宝塔php安装那个合_使用宝塔面板安装nextcloud | 启用本地存储 | 安装smbclient
  3. 在c语言中数组下标的最小值,数组元素下标的上限_c语言中,数组元素的下标下限为...
  4. mysql报错级别_MySQL启动出现几个警告级别错误
  5. android下挂串口中断,请大神看看为啥串口中断无法打断定时器中断
  6. ci mysql pdo_CI框架中pdo的使用方法
  7. sqoop同步时间戳到mysql_在sqoop导入中使用24小时时间戳
  8. 【JAVA基础篇】面对对象的特征
  9. Web项目:解决eclipse中jsp文件报错javax.servlet.http.HttpServlet 提示 was not found on the Java Build Path的错误
  10. 基于RobotFramework实现自动化测试