7615. 9.10 Anagrams

Description

Rewrite Exercise 7.18 using the string class with the following function header:

bool isAnagram(const string &s1, const string &s2)

Two words are anagrams if they contain the same letters in any order.

 

自己班的作业写到烦了又跑去蹭别人班的题做……因为这是道改编题,不知道原题的要求是什么,纠结了很久=。=这里的anagram的要求应该是两个字符串具有相同的字符,每种字符的数目一样,总之就就有字符顺序不同而已,大小写还是敏感的,匹配范围也包括非字母

一开始试的时候又是去重又是去大小写敏感又是去非字母字符,都WA,后来试了一下multiset,经过N种组合后发现什么都不去的写法居然过了,然后换了set发现又WA,才明白不需要去重的....试到最后发现只要分别排个序对比一下就行了,好水……

#include<string>
#include<algorithm>
using namespace std;

bool isAnagram( const string &s1, const string &s2 )
{
    string a = s1, b = s2;
    
    sort( a.begin(), a.end() );
    sort( b.begin(), b.end() );
    
    return a == b;
}

 

posted @ 2013-03-16 20:46  Joyee  阅读(258)  评论(0编辑  收藏  举报