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 ...
随机推荐
- 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- 5G时代
电信语音承载在CDMA2G网络--所以2G基本没有网络 网络走fdd4g 如果5G时代来临,4g网络可能就会像3G一样的慢
- node path模块
一.在nodejs中path模块时使用频率很高的模块,其中不乏有很多API写得很模糊,但仔细琢磨下来,也不是很难理解. 1.获取文件所在路径 var path = require('path'); v ...
- 笔记之远程桌面服务(RDS)
Windows默认只能有2个用户同时通过RDP进行连接,非常不方便,于是借此机会学习了下Win2012R2的远程桌面配置.以下我把学习过程记录一下: 1. 最开始我觉得只需要安装“Remote Des ...
- Spring(2):Spring Ioc
1.下载spring-framework-3.2.0 完整包下载路径: https://repo.spring.io/webapp/#/artifacts/browse/tree/Properties ...
- 用vim去掉utf-8 BOM
'去掉utf-8 BOM :set nobomb '保留utf-8 BOM :set bomb
- git gitignore 如何添加,为何添加了无效
需求:一个新项目源码要挂载在GIT服务器上,但是里面的obj文件夹,bin文件夹,.exe文件不提交(每次) 有两种情况出现 1.项目初始化的时候就加入拦截规则文件 gitignore 具体步骤请参 ...
- SpringBoot之mongoTemplate的使用
springboot的版本1.5.17.RELEASE. 1.mongo的IP和端口 在resources下的application.properties中加入如下内容 spring.data.mon ...
- 【BZOJ1072】排列(搜索)
[BZOJ1072]排列(搜索) 题面 BZOJ 洛谷 题解 算下复杂度,如果用\(next\_permutation\) 那就是\(10!\times 10\times 15\),复杂度不太对 那好 ...
- 用Python实现的数据结构与算法:基本搜索
一.顺序搜索 顺序搜索 是最简单直观的搜索方法:从列表开头到末尾,逐个比较待搜索项与列表中的项,直到找到目标项(搜索成功)或者 超出搜索范围 (搜索失败). 根据列表中的项是否按顺序排列,可以将列表分 ...