判断字符串是否为变形词

题目如下:

对于两个字符串A和B,如果A和B中出现的字符种类相同且每种字符出现的次数相同,则A和B互为变形词,请设计一个高效算法,检查两给定串是否互为变形词。

给定两个字符串AB及他们的长度,请返回一个bool值,代表他们是否互为变形词。

测试样例:
"abc",3,"bca",3
返回:true
我的代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;
 bool chkTransform(string A, int lena, string B, int lenb) {
        // write code here
        if(lena!=lenb)return false;
		map<char,int>mapA;
		map<char,int>::iterator itorA;
		map<char,int>mapB;
		map<char,int>::iterator itorB;
		for(int i=0;i<A.length();i++)
		{
			char s=A[i];
			itorA=mapA.find(s);
			if(itorA!=mapA.end())
			{
				itorA->second++;
			}
			else
			{
				mapA.insert(pair<char,int>(s,1));
			}
		}
		for(int i=0;i<B.length();i++)
		{
			char s=B[i];
			itorB=mapB.find(s);
			if(itorB!=mapB.end())
			{
				itorB->second++;
			}
			else
			{
				mapB.insert(pair<char,int>(s,1));
			}
		}
		itorA=mapA.begin();
		itorB=mapB.begin();
		while(itorA!=mapA.end())
		{
			char s=itorA->first;
			itorB=mapB.find(s);
			if(itorB!=mapB.end())
			{
				if(itorB->second==itorA->second)
				{
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
			itorA++;
		}

		return true;

    }

主要用到了STL中的map来实现功能。接下来我会总结一下map的用法和hashmap的用法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值