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;
        }
    

      

  • 相关阅读:
    某公司基于FineBI数据决策平台的试运行分析报告
    perl AnyEvent
    perl 微信 获取消息
    公司里的人际界线——北漂18年(41)
    perl URLencode URLdecode的方法
    Exception:org.eclipse.m2e.wtp.MarkedException: Unable to configure OHBC
    获取DIV内部内容报错
    JsViews Error:Unknown template:“#projectData”
    jQuery获取checkbox选中的值
    nginx mongodb相关配置
  • 原文地址:https://www.cnblogs.com/hustxujinkang/p/4025136.html
Copyright © 2011-2022 走看看