程序员面试金典面试题 01.02. 判定是否互为字符重排

在这里插入图片描述

方法还是数字计数
#include <iostream>
#include<string>
using namespace std;

class Solution {
public:
	bool CheckPermutation(string s1, string s2) {
		int charCount1[100] = { 0 };//全用0进行初始化
		int charCount2[100] = { 0 };//全用0进行初始化
		if (s1.length() != s2.length())
			return false;
		for(int i = 0; i < int(s1.length()); i++){
			charCount1[s1[i] - 'a']++;
			charCount2[s2[i] - 'a']++;
		}
		for (int i = 0; i < 100; i++){//这里这个100用成了s1的长度,下次对象与操作都要写清楚
			if (abs(charCount1[i] - charCount2[i])>0){
				
				return false;
			}
				
		}
		return true;
	}
};
void main(){
	string a = "cuhv"; //C++中严格很多,双引号表示字符串、单引号表示字符
	string c = "cuvs";
	Solution object_f;
	bool b = object_f.CheckPermutation(a,c);
	cout << b << endl;
}


在这里插入图片描述
这道题他们有用位运算、hash表、排序来做的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值