005推断两个字符串是否是变位词 (keep it up)
写一个函数推断两个字符串是否是变位词。
变位词(anagrams)指的是组成两个单词的字符同样,但位置不同的单词。比方说,
abbcd和abcdb就是一对变位词
这也是简单的题。 我们能够排序然后对照, 也能够直接统计字符出现的个数来推断。这里给出统计字符来推断的代码:
bool isAnagram1(const string& vLeft, const string& vRight)
{
if (vLeft.size() != vRight.size()) return false;
int Count[256];
memset(Count, 0, sizeof(Count)); for (unsigned int i=0; i<vLeft.size(); ++i)
{
++Count[vLeft[i]];
--Count[vRight[i]];
} for (unsigned int i=0; i<vLeft.size(); ++i)
{
if (Count[vLeft[i]] !=0) return false;
} return true;
}
005推断两个字符串是否是变位词 (keep it up)的更多相关文章
- str_2.判断两个字符串是否互为旋转词
1. 字符串str的前面任意部分挪到后面形成的字符串叫做字符串str的旋转词 $str1 = "2ab1"; $str2 = "ab12"; $ret = is ...
- 剑指Offer:互为变位词
// 判断两个单词是否互为变位词: 如果两个单词中的字母相同,并且每个字母出现的次数也相同, 那么这两个单词互为变位词 #include <stdio.h> #include <st ...
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- Lintcode--002(两个字符串是变位词)
写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 您在真实的面试中是否遇到过这个题? 样例 给出 s = "abcd", ...
- [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 ...
- java实现字符串匹配问题之求两个字符串的最大公共子串
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- lintcode-158-两个字符串是变位词
158-两个字符串是变位词 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 说明 What is Anagram? Two strings are ...
- [LeetCode] 438. 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 ...
随机推荐
- I - Agri-Net
I - Agri-Net poj 1258 注意:多组数据输入. #include<cstdio> #include<cstring> #include<iostream ...
- layer iframe加载单个图片或者加载页面
加载单个图片 layer.open({ type: , title: false, closeBtn: , area: '150px', skin: 'layui-layer-nobg', //没有背 ...
- cocos2d-x之浅析Hello World
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Directx Matrix.PerspectiveFovLH Matrix.PerspectiveFovRH的理解
该函数一个四个参数public static Matrix PerspectiveFovLH ( float fieldOfViewY, float aspectRatio, float znearP ...
- python fuzzy c-means demo
摘自:http://pythonhosted.org/scikit-fuzzy/auto_examples/plot_cmeans.html#example-plot-cmeans-py,加入了自己的 ...
- Pycharm使用入门
Python安装与Pycharm使用入门 一.安装Python 1.Linux下安装 一般系统默认已安装2.6.6版本,升级成2.7版本, 但 2.6 不能删除,因为系统对它有依赖,epel源里最新的 ...
- 7.matlab字符串分析
1 字符串处理函数 clc; clear all; str='My name is Robin.'; disp(str); %字符串的输出 str_size=size(str) %字符串的长度 str ...
- POJ 2346 DP or打表
这题 不算重复的数.. 就变成水题了. 思路: 1.打表 2.DP 打表的: // by SiriusRen #include <cstdio> using namespace std; ...
- POJ 3260 DP
只需要对John的付款数做一次多重背包,对shopkeeper的找零钱数做一次完全背包即可. 最重要的是上界的处理.可以注意到,John的付款数最多为maxv*maxv+m,也就是24400元.同理, ...
- C++中的pair,make_pair学习
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型.例如std::pair<int,float> 或者 std::pair<double,do ...