lintcode-158-两个字符串是变位词
158-两个字符串是变位词
写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
说明
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
样例
给出 s = "abcd",t="dcab",返回 true.
给出 s = "ab", t = "ab", 返回 true.
给出 s = "ab", t = "ac", 返回 false.挑战
O(n) time, O(1) extra space
标签
字符串处理 Cracking The Coding Interview
思路
可以先将 2 个字符串排序,然后逐个对比字符串每位是否相同,但排序需要 O(nlogn) 的时间复杂度
所以可以使用 hash 的方式,开辟 2 个 128 位的空间,记录 2 个字符串每种字符出现的次数,然后比较次数是否相同,若相同,是变位词,不同,则不是变位词
code
class Solution {
public:
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
bool anagram(string s, string t) {
// write your code here
int sizeS = s.size(), sizeT = t.size() , i = 0;
if (sizeS == 0 && sizeT == 0) {
return true;
}
if (sizeS != sizeT) {
return false;
}
int hashS[128] = { 0 };
int hashT[128] = { 0 };
for (i = 0; i < sizeS; i++) {
hashS[s[i]]++;
hashT[t[i]]++;
}
for (i = 0; i < 128; i++) {
if (hashS[i] != hashT[i]) {
return false;
}
}
return true;
}
};
lintcode-158-两个字符串是变位词的更多相关文章
- Lintcode--002(两个字符串是变位词)
写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 您在真实的面试中是否遇到过这个题? 样例 给出 s = "abcd", ...
- LintCode-两个字符串是变位词
题目描述: 写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的 样例 给出 s="abcd",t="dcab",返回 true ...
- LintCode 158: Anagram
LintCode 158: Anagram 题目描述 写出一个函数anagram(s, t)判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 样例 给出s = "abcd" ...
- 剑指Offer:互为变位词
// 判断两个单词是否互为变位词: 如果两个单词中的字母相同,并且每个字母出现的次数也相同, 那么这两个单词互为变位词 #include <stdio.h> #include <st ...
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 005推断两个字符串是否是变位词 (keep it up)
写一个函数推断两个字符串是否是变位词. 变位词(anagrams)指的是组成两个单词的字符同样,但位置不同的单词.比方说, abbcd和abcdb就是一对变位词 这也是简单的题. 我们能够排序然后对照 ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
随机推荐
- PHP批量清理MIP-cache缓存(内附在线mipcache清理工具)
MIP是什么?我就不多说了把. MIPCache 又是什么? 科普一下:MIPCache 是一套基于代理的 CDN 缓存系统.可用于缓存所有被某度相关页面引用或者从百度相关服务点出的 MIP 页面.当 ...
- python3 urllib爬取wallhalla网站图片
点我去我的github上看源码 简单使用静态方法爬取https://wallhalla.com/网站的图片 参考: https://blog.csdn.net/cquptcmj/article/det ...
- 函数调用方法之__cdecl与_stdcall
在debug VS c工程文件时,碰到cannot convert from 'int (__cdecl *)(char *)' to 'xxx'这个问题,发现问题在于typedef函数指针类型时,将 ...
- linux (ubuntu)安装pycharm
1. 下载 http://www.jetbrains.com/pycharm/download/ 选择Linux Tab,选择下载免费的Community Edition. 2. 安装PyCharm ...
- MongoDB入门---数据库&&&集合的基本操作
MongoDB作为一种nosql的数据库,它自己本身的增伤改查还有数据库集合的创建和展示与一般的数据库较之是有一部分差别的.我们今天就来看一下MongoDB的一些基本操作. 首先呢,就是先来数据 ...
- 算法-PHP实现八大算法
八大算法原理详解 交换函数:注意要按引用传递,否则无法真正交换两个数的值 function exchange(&$a, &$b){ $temp = $a; $a = $b; $b = ...
- 【CF613D】Kingdom and its Cities(虚树,动态规划)
[CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...
- 优步UBER司机全国各地奖励政策汇总 (2月15日-2月21日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【转载】四元数-Quaterion
原文:四元数-Quaterion 四元数(Quaterion) 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循“署名-非商业用途-保持一致”创作公用协议 ...
- 学习Drupal一个容易被忽视的问题
刚刚修复了一个问题,一个非常小的问题,但我花了2-3小时才查明原因并修复. 总结下来我忽视了一个非常常见的问题或者没有养成一个好的习惯. 问题现象是:论坛发帖,只有editor以上权限的人可以发帖,也 ...