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 = “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;
}
};
LeetCode(242)Valid Anagram的更多相关文章
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(49)-Valid Parentheses
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode(125) Valid Palindrome
题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
随机推荐
- HDU 5875 H - Function 用单调栈水过了
http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...
- 各种安卓模拟器连接Adb
夜神模拟器:adb connect 127.0.0.1:62001 逍遥安卓模拟器:adb connect 127.0.0.1:21503 天天模拟器:adb connect 127.0.0.1:65 ...
- @PropertySource
功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和 @ConfigurationProperties 使用. @Prope ...
- 9、调整数组顺序使奇数位于偶数前面------------>剑指offer系列
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 思路 首先寻找第一个 ...
- Kendo UI Validator 概述
Kendo UI Validator 概述 Kendo UI Validator 支持了客戶端校驗的便捷方法,它基於 HTML 5 的表單校驗功能,支持很多內置的校驗規則,同時也提供了自定義規則的便捷 ...
- Android 4.4及以后将内容布局延伸到状态栏
首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示: 界面 这个界面的布局就是co ...
- android配置android studio not found target android-*.的问题
列:not found target android-25, 打开下载android SDK的工具栏,找到android-25版本下载到你本地的sdk路径下就OK了.
- 【js类库AngularJs】解决angular+springmvc的post提交问题
[js类库AngularJs]解决angular+springmvc的post提交问题 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前 ...
- 初识SeekBar
SeekBar拖动条,是Progress的间接子类 <SeekBar android:id="@+id/seekBar1" android:layout_width=&quo ...
- SharePoint 2013 安装配置(1)
在这篇文章中,我将逐步介绍在Windows Server 2012 R2上安装SharePoint 2013. 在进一步详细介绍之前,让我们先了解SharePoint 2013安装的硬件和软件要求.您 ...