zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    0426-mysql插入语句大全
    JS节点操作
    模态框
    滚动监听 after选择器
    JS数组
    js函数 DOM操作
    JS循环 for while 全局/局部变量 短路
    JavaScript 基础 if switch 弹窗 运算符
    无序列表属性 隐藏方式 JS简介
    Css问题 margin float 文档流 背景图底部充满
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2963785.html
Copyright © 2011-2022 走看看