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 ...
随机推荐
- 微信小程序 功能函数 获取验证码*
yanZhengInput: function (e) { var that = this; var yanzheng = e.detail.value; var huozheng = this.da ...
- es6 很简单
es6出了许多好的,优秀的特性.下面列举一些常用的 其实这些特性都很好理解,一两句话就可以表达出来看.主要是对旧的写法的一种改进. function 加了一些语言糖,传参更方便 class ...
- UVAlive4080_Warfare And Logistics
给一个无向图,求出两个值,所有点到所有其他点的最短距离和,任意删除一条边后的这个值. 数据规模是100点1000边. 白书例题,不多说了直接对于每个点求出最短路树,对于每条边,如果它不是最短路树上的边 ...
- c语言基本数据类型(short、int、long、char、float、double)
一 C 语言包含的数据类型 short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 在不同的系统上,这些类型占据的字节长度是不同的: 在32 ...
- 百度地图经纬度批量查找功能XGeocoding使用手册
<XGeocoding使用手册> 1.下载XGeocoding V2 http://www.gpsspg.com/xgeocoding/download/ 2.解压XGeocoding_v ...
- 【BZOJ1853】幸运数字(搜索,容斥)
[BZOJ1853]幸运数字(搜索,容斥) 题面 BZOJ 洛谷 题解 成功轰下洛谷rk1,甚至超越了一个打表选手 这题思路很明显吧,先搞出来所有范围内的合法数字,然后直接容斥, 容斥的话显然没有别的 ...
- codevs2875RY哥查字典
题目链接:http://codevs.cn/problem/2875/ 题目描述 Description RY哥最近新买了一本字典,他十分高兴,因为这上面的单词都十分的和谐,他天天查字典. 输入描述 ...
- cgroup限制内存
cgroup有个memory子系统,有两组对应的文件,一组带 memsw ,另一组不带. # docker ps -a # cd /sys/fs/cgroup/memory/docker/4b5619 ...
- 封装个StringBuffer,用array join的方式拼接字符串
(function(window) { var core_ArrPro = Array.prototype; var core_slice = core_ArrPro.slice; var core_ ...
- k最近邻算法(kNN)
from numpy import * import operator from os import listdir def classify0(inX, dataSet, labels, k): d ...