《程序员面试金典》确定两串乱序同构
来源:互联网 发布:网络防伪查询系统 编辑:程序博客网 时间:2024/04/30 01:35
【 声明:版权所有,转载请标明出处,请勿用于商业用途。 联系信箱:libin493073668@sina.com】
题目链接:http://www.nowcoder.com/practice/164929d4acd04de5b0ee2d93047b3b20?rp=1&ru=/ta/cracking-the-coding-interview&qru=/ta/cracking-the-coding-interview/question-ranking
题目描述
给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。
给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。
测试样例:
"This is nowcoder","is This nowcoder"
返回:true
"Here you are","Are you here"
返回:false
思路
使用哈希统计两个字符串中每个字符的个数,最后再来比较,一旦有不相等的,自然就返回false
class Same{public:bool checkSam(string stringA, string stringB){// write code hereint lenA = stringA.length();int lenB = stringB.length();if(lenA!=lenB)return false;int cntA[256] {0},cntB[256] {0};for(int i = 0; i<lenA; i++)cntA[stringA[i]]++;for(int i = 0; i<lenB; i++)cntB[stringB[i]]++;for(int i = 0; i<256; i++)if(cntA[i]!=cntB[i])return false;return true;}};
0 0
- 《程序员面试金典》--确定两串乱序同构
- 《程序员面试金典》确定两串乱序同构
- 【程序员面试金典】确定两串乱序同构
- 【程序员面试金典】确定两串乱序同构
- 确定两串乱序同构(程序员面试金典)
- 程序员面试金典 1.3 确定两串乱序同构
- 程序员面试金典题解 确定两串乱序同构
- 程序员面试金典——确定两串乱序同构
- 程序员面试金典(3):确定两串乱序同构(python)
- 确定两串乱序同构之程序员面试经典
- 程序员面试金典第一章:数组与字符串(3) 确定两串乱序同构
- 《Cracking the Coding Interview程序员面试金典》----确定两串乱序同构
- 程序员面试金典:字符串--确定两串乱序同构、基本字符串压缩
- 面试金典系列3--确定两串乱序同构
- 确定两串乱序同构
- 确定两串乱序同构
- 确定两串乱序同构
- 牛客网:确定两串乱序同构
- hive之HQL用法案例
- Randow Id generator with array of probability
- qml学习-------------row布局
- a 标签 禁用
- linux之gcc基础
- 《程序员面试金典》确定两串乱序同构
- Android开发之Android 5.0新特性
- linux内核定时器编程
- Session的生命周期
- php 与 java socket通信问题
- SQL行转列汇总--通用方法
- 程序员面试之循环与递归(四)
- Mysql的jdbc.properties模板
- Java读取war包文件