Question

In: Computer Science

Write the following Python script: Problem Statement A string X is an anagram of string Y...

Write the following Python script:

Problem Statement

A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb".

A set of strings is anagram-free if it contains no pair of strings which are anagrams of each other. Given a set of strings words, return the size of its largest anagram-free subset. Note that the entire set is considered a subset of itself.

Constraints

  • words will contain between 1 and 50 elements, inclusive
  • Each element of words contains between 1 and 50 characters, inclusive
  • Each element of words will consist of lowercase letters 'a' -- 'z' only
  • All elements of words will be distinct

Check your code with these examples:

------------------------------------------
words = ["abcd","abdc","dabc","bacd"]
Returns: 1
All of the strings in words are anagrams of each other, so no two of them can simultaneously belong to an anagram-free set.

------------------------------------------

words = ["a"]

Returns: 1

------------------------------------------

words = ["z"]

Returns: 1

------------------------------------------

words = ["abcd","abac","aabc","bacd"]

Returns: 2

One of the maximum anagram-free subsets is ["abcd","abac"].

------------------------------------------
words: ["aa","aaaaa","aaa","a","bbaaaa","aaababaa"]

Returns: 6

Note that strings of different length cannot be anagrams of each other.

------------------------------------------

words = ["creation","sentence","reaction","sneak","star","rats","snake"]

Returns: 4

Solutions

Expert Solution

Program Code Screenshot



Program Sample Console Input/Output Screenshot



Program Code to Copy

# Algorithm
# Sort each word by characters
# sort the word array
# remove all duplicates from the word array
def largestAnagramSubset(words):
    # Sort each word by characters
    for i in range(len(words)):
        words[i] = ''.join(sorted(words[i]))

    # sort words
    words.sort()

    # remove duplicates
    cur = 1
    while(cur < len(words)):
        if(words[cur] == words[cur-1]):
            words.pop(cur)
        else:
            cur += 1

    return len(words)

# demo


print("words = [\"abcd\", \"abdc\", \"dabc\", \"bacd\"]\nReturns:",
      largestAnagramSubset(["abcd", "abdc", "dabc", "bacd"]), "\n")

print("words = [\"a\"]\nReturns:", largestAnagramSubset(["a"]), "\n")

print("words = [\"z\"]\nReturns:", largestAnagramSubset(["z"]), "\n")

print("words = [\"abcd\",\"abac\",\"aabc\",\"bacd\"]\nReturns:",
      largestAnagramSubset(["abcd", "abac", "aabc", "bacd"]), "\n")

print("words = [\"aa\",\"aaaaa\",\"aaa\",\"a\",\"bbaaaa\",\"aaababaa\"]\nReturns:",
      largestAnagramSubset(["aa", "aaaaa", "aaa", "a", "bbaaaa", "aaababaa"]), "\n")

print("words = [\"creation\",\"sentence\",\"reaction\",\"sneak\",\"star\",\"rats\",\"snake\"]\nReturns:",
      largestAnagramSubset(["creation", "sentence", "reaction", "sneak", "star", "rats", "snake"]), "\n")

Related Solutions

Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write a script that will first initialize a string variable that will store x and y...
Write a script that will first initialize a string variable that will store x and y coordinates of a point in the form ‘x 3.1 y 6.4’. Then, use string manipulating functions to extract the coordinates and plot them. ANS % create a string variable of the form 'x 3.1 y 6.4' % extract the x and y coordinates and plot str = 'x 2.3 y 4.5'; [letter rest] = strtok(str); [x rest] = strtok(rest); [letter rest] = strtok(rest); y...
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
Write the following Python script: Problem Statement We want to know how many different people have...
Write the following Python script: Problem Statement We want to know how many different people have eaten at a restaurant this past week. The parameter meals has strings in the format "name:restaurant" for a period of time. Sometimes a person eats at the same restaurant often. Return the number of different people who have eaten at the eating establishment specified by parameter restaurant. For example, "John Doe:Moes" shows that John Doe ate one meal at Moes. Write function howMany that...
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT