zoukankan      html  css  js  c++  java
  • Anagrams

    这题折腾主要是因为对于这个anagram的定义理解有误,参看维基页面:

    An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example Torchwood can be rearranged into Doctor Who. Someone who creates anagrams may be called an "anagrammatist".[1] The original word or phrase is known as thesubject of the anagram.

    Any word or phrase that exactly reproduces the letters in another order is an anagram. However, the goal of serious or skilled anagrammatists is to produce anagrams that in some way reflect or comment on the subject. Such an anagram may be a synonym or antonym of its subject, a parody, a criticism, or praise; e.g. William Shakespeare = I am a weakish speller

    Another example is "silent" which can be rearranged to "listen". The two can be used in the phrase, "Think about it, SILENT and LISTEN are spelled with the same letters". (To mean "the quieter you become, the more you can hear").

    简单解释就是说,两个单词word1, word2,构成他们的字符个数和种类都相同,那就是满足anagram。

    实际实现就是,如果word2能够通过调整字符顺序得到word1,那就成立了。

        vector<string> anagrams(vector<string> &strs) {
        map<string, int> table;
    	for (auto it = strs.begin(); it != strs.end(); it++){
    		string s = *it;
    		sort(s.begin(), s.end());
    		if (table.find(s) == table.end()){
    			table[s] = 1;
    		}
    		else {
    			table[s]++;
    		}
    	}
    	vector<string> result;
    	for (auto it = strs.begin(); it != strs.end(); it++){
    		string s = *it;
    		sort(s.begin(), s.end());
    		if (table[s] > 1)
    			result.push_back(*it);
    	}
    	return result;
        }
    

      

  • 相关阅读:
    通过Logstash由SQLServer向Elasticsearch同步数据
    ELK +Nlog 分布式日志系统的搭建 For Windows
    Gulp 给所有静态文件引用加版本号
    Fluentdata详解
    jQuery Easy UI (适应屏幕分辨率大小)布局(Layout)
    什么是数据结构---算法
    WCF的学习之旅
    程序员必学之精华----软件工程
    译 .NET Core 3.0 发布
    Vue+Element UI 实现视频上传
  • 原文地址:https://www.cnblogs.com/hustxujinkang/p/4025136.html
Copyright © 2011-2022 走看看