简体   繁体   中英

Merging and sorting n strings in O(n)

I recently was given a question in a coding challenge where I had to merge n strings of alphanumeric characters and then sort the new merged string while only allowing alphabetical characters in the sorted string. Now, this would be fairly straight forward except that the caveat added was that the algorithm had to be O(n) (it didn't specify whether this was time or space complexity or both).

My initial approach was to concatenate the strings into a new one, only adding alphabetical characters and then sorting at the end. I wanted to come up with a more efficient solution but I was given less time than I was initially told. There isn't any sorting algorithm (that I know of) which runs in O(n) time, so the only thing I can think of is that I could increase the space complexity and use a sorted hashtable (eg C++ map) to store the counts of each character and then print the hashtable in sorted order. But as this would require possibly printing n characters n times, I think it would still run in quadratic time. Also, I was using python which I don't think has a way to keep a dictionary sorted (maybe it does).

Is there anyway this problem could have been solved in O(n) time and/or space complexity?

Your counting sort is the way to go: build a simple count table for the 26 letters in order. Iterate through your two strings, counting letters, ignoring non-letters. This is one pass of O(n). Now, simply go through your table, printing each letter the number of times indicated. This is also O(n), since the sum of the counts cannot exceed n . You're not printing n letters n times each: you're printing a total of n letters.

  1. Concatenate your strings (not really needed, you can also count chars in the individual strings)
  2. Create an array with length equal to total nr of charcodes
  3. Read through your concatenated string and count occurences in the array made at step 2
  4. By reading through the char freq array, build up an output array with the right nr of repetitions of each char.

Since each step is O(n) the whole thing is O(n)

[@patatahooligan: had made this edit before I saw your remark, accidentally duplicated the answer]

If I've understood the requirement correctly, you're simply sorting the characters in the string?

Ie ADFSACVB becomes AABCDFSV ?

If so then the trick is to not really "sort". You have a fixed (and small) number of values. So you can simply keep a count of each value and generate your result from that.

Eg Given ABACBA

In the first pass, increment a counters in an array indexed by characters. This produces:

[A] == 3
[B] == 2
[C] == 1

In second pass output the number of each character indicated by the counters. AAABBC

In summary, you're told to sort , but thinking outside the box, you really want a counting algorithm.

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