anagram

Finding and grouping anagrams by Python

空扰寡人 提交于 2019-12-20 01:35:30
问题 input: ['abc', 'cab', 'cafe', 'face', 'goo'] output: [['abc', 'cab'], ['cafe', 'face'], ['goo']] The problem is simple: it groups by anagrams . The order doesn't matter. Of course, I can do this by C++ (that's my mother tongue). But, I'm wondering this can be done in a single line by Python . EDITED: If it's not possible, maybe 2 or 3 lines. I'm a newbie in Python. To check whether two strings are anagram, I used sorting. >>> input = ['abc', 'cab', 'cafe', 'face', 'goo'] >>> input2 = [''.join

Check whether two strings are anagrams using C++

天大地大妈咪最大 提交于 2019-12-18 13:47:32
问题 The program below I came up with for checking whether two strings are anagrams. Its working fine for small string but for larger strings ( i tried : listened , enlisted ) Its giving me a 'no !' Help ! #include<iostream.h> #include<string.h> #include<stdio.h> int main() { char str1[100], str2[100]; gets(str1); gets(str2); int i,j; int n1=strlen(str1); int n2=strlen(str2); int c=0; if(n1!=n2) { cout<<"\nThey are not anagrams ! "; return 0; } else { for(i=0;i<n1;i++) for(j=0;j<n2;j++) if(str1[i]

Anagram of String 2 is Substring of String 1

China☆狼群 提交于 2019-12-18 07:12:44
问题 How to find that any anagram of String 1 is sub string of String 2? Eg :- String 1 = rove String 2= stackoverflow So it will return true as anagram of "rove" is "over" which is sub-string of String 2 回答1: On edit: my first answer was quadratic in the worst case. I've tweaked it to be strictly linear: Here is an approach based on the notion of a sliding window: Create a dictionary keyed by the letters of the first dictionary with frequency counts of the letters for the corresponding values.

Optimizing very often used anagram function

倖福魔咒の 提交于 2019-12-17 18:27:06
问题 I have written a function that determines whether two words are anagrams. Word A is an anagram of word B if you can build word B out of A just by rearranging the letters, e.g.: lead is anagram of deal This is my function: bool is_anagram(std::string const & s1, std::string const & s2) { auto check = [](std::string const & x) { std::map<char, unsigned> counter; for(auto const & c : x) { auto it = counter.find(c); if(it == counter.end()) counter[c] = 1; else ++counter[c]; } return counter; };

A possible algorithm for determining whether two strings are anagrams of one another? [closed]

微笑、不失礼 提交于 2019-12-17 12:12:36
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have this idea (using C language) for checking whether two strings formed from ASCII letters are anagrams of one another: Check if the strings are the same length. Check if the sum of the ASCII values of all chars is the same for both strings. Check if the product of the ASCII values of all chars is the same

A possible algorithm for determining whether two strings are anagrams of one another? [closed]

不打扰是莪最后的温柔 提交于 2019-12-17 12:12:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have this idea (using C language) for checking whether two strings formed from ASCII letters are anagrams of one another: Check if the strings are the same length. Check if the sum of the ASCII values of all chars is the same for both strings. Check if the product of the ASCII values of all chars is the same

Anagram algorithm in java

给你一囗甜甜゛ 提交于 2019-12-17 07:34:11
问题 I would like to make anagram algorithm but This code doesn't work. Where is my fault ? For example des and sed is anagram but output is not anagram Meanwhile I have to use string method. not array. :) public static boolean isAnagram(String s1 , String s2) { String delStr=""; String newStr=""; for(int i=0;i<s1.length();i++) { for(int j=0 ; j < s2.length() ; j++) { if(s1.charAt(i)==s2.charAt(j)) { delStr=s1.substring(i,i+1); newStr=s2.replace(delStr,""); } } } if(newStr.equals("")) return true;

LeetCode:Valid Anagram

a 夏天 提交于 2019-12-15 20:25:43
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、题目名称 Valid Anagram (易位构词) 2、题目地址 https://leetcode.com/problems/valid-anagram/ 3、题目内容 英文:Given two strings s and t, write a function to determine if t is an anagram of s. 中文:给出两个字符串,写一个函数判断t是否是s的易位构词 例如: s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. 4、题目分析 易位构词游戏的规则可以参考相关的 维基百科页面 :易位构词是一类文字游戏。它的规则是将组成一个词或短句的字母重新排列顺序,原文中所有字母的每次出现都被使用一次,这样构造出另外一些新的词或短句。现在我们要判断的就是给出两个字符串s和t,判断字符串t能否由s通过易位构词产生。 5、一个超时的方法 在做这个题的时候我第一个想到的方法就是,将s和t两个字符串先转换成数组,然后分别对两个数组进行排序,再对两个排序后的数组进行逐位比较。如果发现两个数组中不一致的地方,则返回false,否则返回true。对应的Java代码如下: /** * 功能说明

make anagrams generator in javascript finding only words contained in a list

萝らか妹 提交于 2019-12-13 05:04:22
问题 I'm trying to improve this anagrams generator in javascript: JS: var str,re,cnt,cntmax; function rst(){ cnt=0; cntmax=10000; str=""; if (document.forms[0].re.value != ""){ re=new RegExp(document.forms[0].re.value); } else { re=null; } } function go(prefix, postfix){ if (cnt>=cntmax) return; if (postfix==""){ if (re==null || prefix.match(re)==null){ str+=prefix+"\r\n"; cnt++; } return; } for (var i=0;i<postfix.length;i++){ var prefix2=prefix+postfix.charAt(i); var postfix2=postfix.substring(0

Create lists of anagrams from a list of words

▼魔方 西西 提交于 2019-12-12 16:26:41
问题 I want to find create lists of anagrams from a list of words. Should I use another loop in my code or recursion? some_list = ['bad', 'app', 'sad', 'mad', 'dab','pge', 'bda', 'ppa', 'das', 'dba'] new_list = [some_list[0]] i = 0 while i+1 < len(some_list): if (''.join(sorted(some_list[0]))) == (''.join(sorted(some_list[i+1]))): new_list.append(some_list[i+1]) i = i+1 else: i = i+1 print(new_list) My output is ['bad', 'dab', 'bda', 'dba'] . But I also want more lists of other anagrams from some