题目要求是,输入两个字符串,s和t,比较俩个字符串的连接模式是否相同。其中字符串连接模式可以定义为,字符串中字符的出现规律。
例如:
feed 和 goot,其中ee和oo出现位置相同,所以其拼接模式相同;
paper和title,paper中的p和titile中的t出现位置相同;
newspaper和 dog,字符串长度相同,所以拼接模式不可能相同;
zoo和aac, oo连续出现在位置2和3,但在aac中却没有对应相同的字符出现,所以其模式不同.
要求实现函数bool fun(const string & s, const string & t);
解答:可以通过映射来实现,代码如下:
bool fun(const string & s, const string & t)
{
if (s.size() != t.size())return false;
int mp[300] = { 0 };
for (int i = 0;i < t.size(); ++i)
{
if (!mp[s[i]])mp[s[i]] = t[i];
else if (mp[s[i]] != t[i])return false;
}
return true;
}