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 = "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相关推荐
- 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 = & ...
- 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 ...
- Leetcode题目:Valid Anagram
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- [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, ...
- LeetCode OJ -- Binary Tree Paths
http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...
- LeetCode 32. Longest Valid Parentheses
问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...
- LeetCode算法入门- Valid Parentheses -day11
LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...
- [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 ...
- [LeetCode] Valid Anagram - 字符串排序比较系列
题目概述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
最新文章
- Windows Server 2008壮烈牺牲
- 【ros】4.rosbag的相关用法
- api key for Alpha Vantage
- js父页面和子页面之间传值
- html5 梵高 星,梵高作品欣赏《星空》
- DCMTK:将DICOM文件的内容转换为JSON格式
- (十)java springboot b2b2c shop 多用户商城系统源码:服务网关zuul初级篇
- 我被喷了:这样写代码是多此一举?
- visualstudiopython使用方法,使用python解析VisualStudio .csproj文件的最佳方法
- div垂直,水平居中
- arcgis中img 批量转换tif_在ArcGIS中Python使用及批量裁剪示技巧讲解 !
- 查看exe代码_【安全风险通告】Windows Type 1字体解析远程代码执行漏洞安全风险通告...
- php的慢查询,慢查询是什么
- 测试用例的定义,测试用例模板,测试用例的作用,测试用例编写注意事项
- android实现图片自动轮播代码,Android实现图片轮播切换实例代码
- 在虚拟机上安装使用LoadRunner教程
- Debian修改IP地址
- 关于OSPF的区域划分规则
- 基于单片机的电机转速测量设计
- 强化学习15——Proximal Policy Optimization (PPO)算法详细推导
热门文章
- 如何更新mysql数据库字段_如何使用MySQL一个表中的字段更新另一个表中字段
- 宝塔php安装那个合_使用宝塔面板安装nextcloud | 启用本地存储 | 安装smbclient
- 在c语言中数组下标的最小值,数组元素下标的上限_c语言中,数组元素的下标下限为...
- mysql报错级别_MySQL启动出现几个警告级别错误
- android下挂串口中断,请大神看看为啥串口中断无法打断定时器中断
- ci mysql pdo_CI框架中pdo的使用方法
- sqoop同步时间戳到mysql_在sqoop导入中使用24小时时间戳
- 【JAVA基础篇】面对对象的特征
- Web项目:解决eclipse中jsp文件报错javax.servlet.http.HttpServlet 提示 was not found on the Java Build Path的错误
- 基于RobotFramework实现自动化测试