anagram

Searching anagrams with Java 8

我只是一个虾纸丫 提交于 2021-02-08 06:14:27
问题 I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000. Output example: word anagram1 anagram2 anagram3 ... word2 anagram1 anagram2... I have code, it works but very slow: private static List<String> listOfWords = new ArrayList<String>(); private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>(); public static void main(String[] args) throws Exception {

Searching anagrams with Java 8

我的未来我决定 提交于 2021-02-08 06:09:51
问题 I have to write program which should be reading file for anagrams and show word + his anagrams. Txt files is very big, after using scanner, listOfWords size is: 25000. Output example: word anagram1 anagram2 anagram3 ... word2 anagram1 anagram2... I have code, it works but very slow: private static List<String> listOfWords = new ArrayList<String>(); private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>(); public static void main(String[] args) throws Exception {

Creating anagram detector

爷,独闯天下 提交于 2021-01-29 02:23:49
问题 I'm having trouble getting this anagram function to work. The aim is for the function to take 2 strings abc and cba , convert them into a list; sort them in to alphabetical order, compare the elements of the list and print whether they are anagrams or not. My code is as follows... def anagram(str1, str2): x = str1 y = str2 x1 = x.sort() y1 = y.sort() if (x1) == (y1): print("Anagram is True") else: print("Anagram is False") str1 = str('abc') str2 = str('cba') print(anagram(str1, str2)) 回答1:

Find all the possible N-length anagrams - fast alternatives

為{幸葍}努か 提交于 2021-01-28 07:43:10
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

Find all the possible N-length anagrams - fast alternatives

♀尐吖头ヾ 提交于 2021-01-28 07:21:18
问题 I am given a sequence of letters and have to produce all the N-length anagrams of the sequence given, where N is the length of the sequence. I am following a kinda naive approach in python, where I am taking all the permutations in order to achieve that. I have found some similar threads like this one but I would prefer a math-oriented approach in Python. So what would be a more performant alternative to permutations? Is there anything particularly wrong in my attempt below? from itertools

String permutations using recursion in Java

六月ゝ 毕业季﹏ 提交于 2021-01-27 06:45:31
问题 I came across THIS post which tried very hard to explain the recursive solution to print all string. public class Main { private static void permutation(String prefix, String str){ int n = str.length(); if (n == 0) System.out.println(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1)); } } public static void main(String[] args) { permutation("", "ABCD"); } } But still I am not able to get the part when we start popping off

What is the fast way to find all anagrams inside an array in objective c?

不想你离开。 提交于 2020-01-16 08:06:19
问题 I have an array that contains multiple words in there. The problem is asking me to find all the anagrams in there and pick out which one has the most anagrams. This is what I have so far: Create a duplicate array of the old array. Create an index array for the old array, to keep track of where every word is located. This index value won't be changed throughout the program For the new array, I will be sort each individual word in the array. For example: Before sorting, array has: [cat dog tac

Find if 2 strings are anagram in O(1) space and O(n) time

老子叫甜甜 提交于 2020-01-13 12:13:26
问题 You can find if 2 strings are anagrams after sorting both strings in O(nlogn) time, however is it possible to find it in o(n) time and O(1) space. 回答1: Absolutely no expert here... But why not go through each string and simply count how many times each letter turns up. Given appropriate implementation, this shouldn't take more than O(n) time. 回答2: generate a prime number array[26] each prime number represent a character, then when you traverse the string, multiple each character's prime

Need help expanding on an anagram regex

时光毁灭记忆、已成空白 提交于 2020-01-06 04:37:09
问题 I am attempting to expand on this regex for listing all possible anagrams for a given set of letters: ^(?!.*([aer]).*\1)(?!(.*d){4})([aerd]*|[a-z])$ so far based on this regex, I can receive a match on any combination of words and sub-words made up of the letters 'dadder', such as 'adder', 'add', 'ad', 'red' etc. The reason for the regex complexity instead of a simple [dadder]* is because obviously each letter can be matched an infinite amount of times, which is bad, I want each letter to

Using PHP write an anagram function?

≯℡__Kan透↙ 提交于 2020-01-02 15:24:31
问题 Using PHP write an anagram function? It should be handling different phrases and return boolean result. Usage: $pharse1 = 'ball'; $pharse2 = 'lbal'; if(is_anagram($pharse1,$pharse2)){ echo $pharse1 .' & '. $pharse2 . ' are anagram'; }else{ echo $pharse1 .' & '. $pharse2 . ' not anagram'; } 回答1: There's simpler way function is_anagram($a, $b) { return(count_chars($a, 1) == count_chars($b, 1)); } example: $a = 'argentino'; $b = 'ignorante'; echo is_anagram($a,$b); // output: 1 $a = 'batman'; $b