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的更多相关文章

  1. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  2. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. [LeetCode] Valid Anagram 验证变位词

    Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...

  4. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  5. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  6. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  7. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  8. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  9. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

随机推荐

  1. json 数组

    /** * 获取 选中的id * @returns {*} */function getCheckedList(){ var groups =[]; $("input[name='check ...

  2. c语言文法定义

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  3. sys下gpio操作

    gpio_operation 通过/sys/文件接口操作IO端口 GPIO到文件系统的映射 * 控制GPIO的目录位于/sys/class/gpio * /sys/class/gpio/export文 ...

  4. lucene介绍

    1.https://blog.csdn.net/shuaicihai/article/details/65111523 2.https://www.cnblogs.com/rodge-run/p/65 ...

  5. how to get iframe dom in js

    how to get iframe dom in js https://stackoverflow.com/questions/3999101/get-iframes-document-from-ja ...

  6. matlab dist函数

    dist——欧式距离加权函数(Euclidean distance weight function) 语法: Z = dist(W,P)    df = dist('deriv')    D = di ...

  7. 【大数据】关于Kafka的进一步理解

    前置: 文件host 192.168.11.13 192.168.11.14 192.168.11.30 脚本init_kafka.sh #!/bin/bash source /etc/profile ...

  8. unable to create new native thread

    一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题:运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclipse ...

  9. easyui的datebox只显示年月

    要求点击easyui的datebox时只显示年月,隐藏日,之前百度了好多,发现有的好麻烦,有的使用没效果,也许自己没理解,改不了.最后老员工帮我搞定了,添加一个fomatter和一个parser函数就 ...

  10. 洛谷 P4301 [CQOI2013]新Nim游戏 解题报告

    P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...