anagram

How do I find words that only contain/consist of a given char sequence

99封情书 提交于 2019-12-06 07:12:31
问题 I wish to loop through a dictionary file and find words that contain only the given characters Example dgo Desired results: dog, god NOT words that contain (within them) the given characters I am working with the following code: while((dictionaryWord = br_.readLine()) != null) { if(dictionaryWord.contains(getWord())) System.out.println(dictionaryWord); } But this gives me all words which contain the given characters -- NOT DESIRED 回答1: Without regular expressions: public static boolean

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

安稳与你 提交于 2019-12-05 16:28:29
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. 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. generate a prime number array[26] each prime number represent a character, then when you traverse the string, multiple each character's prime number, if equal, it is anagrams, otherwise not. it takes O(n) and constant space There are couple of ways to solve it

Regex - find anagrams and sub-anagrams

久未见 提交于 2019-12-05 10:16:05
I have a pool of characters and I want to match all the words which are anagrams of those chars or of a subset of those chars using a regular expression. Example: given the string "ACNE" the regex should give me these results: ACNE [T] CENA [T] CAN [T] CAAN [F] CANEN [F] I've tried this solution /b[acne]{1,4}/b but it accepts multiple repetitions of single chars. What can I do to take each char at most one time? The sub-anagrams of the word "acne" are the words that consist only of the letters acne do not contain a more than once do not contain c more than once do not contain n more than once

Given a string array, return all groups of strings that are anagrams

随声附和 提交于 2019-12-05 07:19:10
Given a string array, return all groups of strings that are anagrams. My solutions: For each string word in the array, sort it O(m lg m), m is the average length of a word. Build up a hash Table < string, list >. Put the sorted word into the hash table as key and also generate all permutations (O(m!)) of the word, search each permutation in a dictionary (a prefix tree map) with O(m), if it is in the dictionary, put (O(1)) it into the hash table so that all permutated words are put into the list with the same key. Totally, O(n * m * lg m * m!) time and O(n* m!) space , n is the size of the

Anagram Algorithm using a hashtable and/or tries

谁说胖子不能爱 提交于 2019-12-04 23:26:48
I have been searching the internet for awhile now for steps to find all the anagrams of a string (word) (i.e. Team produces the word tame) using a hashtable and a trie. All I have found here on SO is to verify 2 words are anagrams. I would like to take it a step further and find an algorithm in english so that I can program it in Java. For example, Loop through all the characters. For each unique character insert into the hashtable. and so forth. I don't want a complete program. Yes, I am practicing for an interview. If this question comes up then I will know it and know how to explain it not

anagram algorithm

我是研究僧i 提交于 2019-12-04 18:44:41
Which is the best way of generating anagrams for a text(maximum 80 characters length). Example: Input: dog output dog dgo odg ogd gdo god I'm only thinking of a backtracking solution, but that will take a while if the text is longer. Another thought is bulding i trie for all words in dictionary, but the problem doesn't ask for real words. Can somebody point out a minimum time complexity solution? Thank you! The question looks like generating the list of permutations; (Anagrams are a subset of permutations, which form a meaningful word). n! Permutations can be generated in chronological order

Comparing two arrays containing strings for anagrams in Ruby

廉价感情. 提交于 2019-12-04 16:10:09
Forgive me if my code is off. I've still got my head in Ruby on Rails which seems to have subtle differences that are coming out as I learn more "just Ruby," although to be fair I'm not sure my code would pass muster in a Ruby on Rails format. I digress. I am trying to compare two arrays that contain a set of strings. I want to do a couple things. 1) Ensure that the arrays are the same number of words, otherwise the exercise is moot. 2) Compare the first word in an array with only the first word in the second array. In other words, I never want to compare word 1 in array "a" with word 4 in

How can I simplify or clean up this anagram method?

主宰稳场 提交于 2019-12-03 23:05:00
问题 I have a method here that takes an array of strings and groups the ones that are anagrams of each other together, with each group forming a sub-array of the main anagram_groups array. The output is fine but I feel like my code is probably overly-complicated. How could my logic and/or syntax be simplified, short of refactoring things into more methods? def combine_anagrams(words) anagram_groups = [] # For each word in array argument words.each do |word| # Tracking variable for the word word

Code golf: find all anagrams

╄→尐↘猪︶ㄣ 提交于 2019-12-03 08:52:31
问题 A word is an anagram if the letters in that word can be re-arranged to form a different word. Task: The shortest source code by character count to find all sets of anagrams given a word list. Spaces and new lines should be counted as characters Use the code ruler ---------10--------20--------30--------40--------50--------60--------70--------80--------90--------100-------110-------120 Input: a list of words from stdin with each word separated by a new line. e.g. A A's AOL AOL's Aachen Aachen's

Anagrams finder in javascript

ぐ巨炮叔叔 提交于 2019-12-03 07:14:33
问题 I am supposed to write a program in JavaScript to find all the anagrams within a series of words provided. e.g.: "monk, konm, nkom, bbc, cbb, dell, ledl, llde" The output should be categorised into rows: 1. monk konm, nkom; 2. bbc cbb; 3. dell ledl, llde; I already sorted them into alphabetical order i.e.: "kmno kmno bbc bbc dell dell" and put them into an array. However I am stuck in comparing and finding the matching anagram within the array. Any help will be greatly appreciated. 回答1: