简体   繁体   中英

Algorithm Complexity: Time and Space

I have two solutions to one coding problem. Both of the solutions work fine. I just need to find the time/space complexity for both of them.

Question: Given a string or words, return the largest set of unique words in the given string that are anagrams of each other.

Example:
Input: 'I am bored and robed, nad derob'
Correct output: {bored, robed, derob}
Wrong output: {and, nad}

Solution 1: In the first solution, I iterate over the given string of words, take each word, sort the characters in it, add it to a dictionary as a key. And the original word (not sorted) is being added to a set of words as a value for that key. Sort helps figure out the words that are anagrams of each other. At each iteration, I also keep track of the key that has the longest set of words as its value. At the end, I return the key that has the longest set.

Solution 2: In the second solution, I do almost the same. The only difference is that I calculate a prime factor for each word to use it as a key in my dictionary. In other words, I don't sort the characters of the word.

def solution_two(string):
    primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
              53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
    d = defaultdict(set)
    longest_set = ''
    for word in string.replace(",", "").replace(".", "").split():
        key = 1
        for ch in word:
            key *= primes[ord(ch.lower()) - 97]
        d[key].add(word)
        if len(d[key]) > len(d[longest_set ]):
            longest_set = key
    return d[longest_set]

My thoughts:

I think that the runtime of the first solution is O(n), where n is the number of words in the string. But if I sort each word of the string, wouldn't it make the runtime O(n) * O(n log n)?

As for the second one, I think that it has the linear runtime too. But I have the second loop inside the first one where I iterate through each character of a word...

I am also confused about the space complexity for both of the solutions.

Any guidance would be greatly appreciated. Thank you.

ALG1= time is O(n)* O(awl log(awl)) and space O(n)*O(awl) but be careful because this looks pretty good but you need to consider that if awl is much smaller than n you get an ~O(n) time while if your awl is bigger you get an O(awl log(awl)) meanwhile worst case of all is if n=awl – ALG2= time O(n)*O(awl), where O(n) is for iterating the given string and O(awl) is calculating the prime factor for each word (ie I take a word, find a prime for each char in the word in the list primes and multiply them) and space O(n) same considerations on n and awl as the previous algorithm

So if i am correct your second algorithm in the WC has a complexity of n² better than the other one also in space!

Just to recap :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM