anagram

clustering words based on their char set

不羁的心 提交于 2019-12-10 14:24:25
问题 Say there is a word set and I would like to clustering them based on their char bag (multiset). For example {tea, eat, abba, aabb, hello} will be clustered into {{tea, eat}, {abba, aabb}, {hello}}. abba and aabb are clustered together because they have the same char bag, i.e. two a and two b . To make it efficient, a naive way I can think of is to covert each word into a char-cnt series, for exmaple, abba and aabb will be both converted to a2b2 , tea/eat will be converted to a1e1t1 . So that

Comparing two arrays containing strings for anagrams in Ruby

ⅰ亾dé卋堺 提交于 2019-12-09 23:00:51
问题 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

Anagram Index Calculation [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-09 06:58:19
问题 This question already has answers here : Find the index of a given permutation in the sorted list of the permutations of a given string (6 answers) Closed 4 years ago . Given an input string up to 25 characters long consisting of characters A-Z, output its index in the alphabetically sorted list of all possible anagrams of the input string. Input string is not case sensitive. Input characters can be repeated. The app must complete in less than 500ms and take less than 1GB of memory. At first

Can't understand the logic behind this anagram problem's solution [closed]

时间秒杀一切 提交于 2019-12-08 11:26:50
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 months ago . Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings. This is my first time preparing for competitive programming and understanding the

Ruby Anagram Using String#sum

点点圈 提交于 2019-12-08 02:52:11
问题 I've solved a problem that asks you to write a method for determining what words in a supplied array are anagrams and group the anagrams into a sub array within your output. I've solved it using what seems to be the typical way that you would which is by sorting the words and grouping them into a hash based on their sorted characters. When I originally started looking for a way to do this I noticed that String#sum exists which adds the ordinals of each character together. I'd like to try and

Quick way to jumble the order of an NSString?

社会主义新天地 提交于 2019-12-07 14:04:29
问题 Does anyone know of an existing way to change the order of an existing NSString or NSMutableString's characters? I have a workaround in mind anyway but it would be great if there was an existing method for it. For example, given the string @"HORSE", a method which would return @"ORSEH", @"SORHE", @"ROHES", etc? 回答1: Consider this code: .h File: @interface NSString (Scrambling) + (NSString *)scrambleString:(NSString *)toScramble; @end .m File: @implementation NSString (Scrambling) + (NSString

Regex - find anagrams and sub-anagrams

守給你的承諾、 提交于 2019-12-07 04:18:59
问题 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? 回答1: The sub-anagrams of the word "acne" are the words that consist only of the

Ruby Anagram Using String#sum

让人想犯罪 __ 提交于 2019-12-06 11:57:39
I've solved a problem that asks you to write a method for determining what words in a supplied array are anagrams and group the anagrams into a sub array within your output. I've solved it using what seems to be the typical way that you would which is by sorting the words and grouping them into a hash based on their sorted characters. When I originally started looking for a way to do this I noticed that String#sum exists which adds the ordinals of each character together. I'd like to try and work out some way to determine an anagram based on using sum . For example "cars" and "scar" are

Using PHP write an anagram function?

岁酱吖の 提交于 2019-12-06 10:34:58
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'; } 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 = 'barman'; echo is_anagram($a,$b); // output (empty): function is_anagram($pharse1,$pharse2){ $status =

Grouping anagrams [duplicate]

戏子无情 提交于 2019-12-06 07:35:56
This question already has answers here : How to check if two words are anagrams (34 answers) Closed 6 years ago . Given array of words, group the anagrams IP:{tar,rat,banana,atr} OP:{[tar,rat,atr],[banana]} One solution to this question using Hash Table. consider each word, sort it and add as key to hash table if not present. The value for the key would be a list of all anagrams with the same key. I wanted to know about the time complexities, To sort the characters in an array, suppose O(n log n) To store in the hash table it would be O(n), a total of O(n*nlogn). Is there a better algorithm?