LintCode 158: Anagram
LintCode 158: Anagram
题目描述
写出一个函数anagram(s, t)判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
样例
给出s = "abcd",t="dcab",返回true.
给出s = "ab", t = "ab", 返回true.
给出s = "ab", t = "ac", 返回false.
Mon Mar 6 2017
思路
这道题很容易想到先将字符串排序,然后比较两个字符串是否相等,这种方法的时间复杂度为\(O(nlogn)\)。
但是题目中有更高的要求,要求时间复杂度为\(O(n)\),空间复杂度为\(O(1)\),所以需要借助哈希表统计各字符出现的次数。
分别统计两个字符串中各字符串出现的次数,若在字符串s出现过,则+1,若在字符串t出现过,则-1,最后检查次数是否全为0即可。
代码
// 两个字符串是变位词
class Solution {
public:
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
bool anagram(string s, string t)
{
if(s.size() != t.size()) return false;
int count[256] = {0};
for (int i = 0; i < s.size(); ++i)
{
++count[s[i]];
--count[t[i]];
}
for (int i = 0; i < 256; ++i)
if (count[i] < 0)
return false;
return true;
}
};
LintCode 158: Anagram的更多相关文章
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
随机推荐
- contos7忘记root密码怎么办
首先在这个界面按"e"键 然后呢就会进入到如下图所示的界面,在LANG=zh_CN.UTF8的后面加上 init=/bin/sh, 再按 [ Ctrl + X ] 进入'单用户模式 ...
- Internet History, Technology and Security (Week 6)
Week 6 Technology: Transport Control Protocol (TCP) Welcome to Week 6 of IHTS. We are in our second ...
- mongodb常用基本命令(根据工作需要,不断更新)
推荐可视化工具:mongobooster 复制库 db.copyDatabase("ability_message","ability_message_cop ...
- ORA-06530: 引用未初始化的组合 ;
select * FROM TABLE(fun_test_1) : ORA-06530: 引用未初始化的组合ORA-06512: 在 "PCISS.FUN_TEST_1", lin ...
- Class.getResourceAsStream 和 ClassLoder.getResourceAsStream 的区别
问题描述 最近学习MyBaits时用到了 InputStream Resources.getResourceAsStream(String resource)来读取MyBatis配置文件,为了方便使用 ...
- POJ1815_Friendship
一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案. 求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数. 需要求出字典序最小的方案,可以从小到大枚举所 ...
- vyos 基础配置
vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...
- hdu2138 Miller_Rabin
Description Give you a lot of positive integers, just to find out how many prime numbers there are ...
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
- 安装elasticsearch5.4.1集群和head插件
这里用的系统版本是CentOS6.6. 192.168.3.56 ES01 192.168.3.49 ES02 192.168.3.57 ES03 1.为三个节点安装java环境 # yum inst ...