Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all unique characters in a string?

I want to append characters to a string, but want to make sure all the letters in the final list are unique.

Example: "aaabcabccd""abcd"

Now of course I have two solutions in my mind. One is using a list that will map the characters with their ASCII codes. So whenever I encounter a letter it will set the index to True. Afterwards I will scan the list and append all the ones that were set. It will have a time complexity of O(n).

Another solution would be using a dict and following the same procedure. After mapping every char, I will do the operation for each key in the dictionary. This will have a linear running time as well.

Since I am a Python newbie, I was wondering which would be more space efficient. Which one could be implemented more efficiently?

PS: Order is not important while creating the list.

like image 958
Ali Avatar asked Dec 16 '12 15:12

Ali


People also ask

How do I get a list of unique characters in a string in Python?

To get unique characters in a given String in Python, pass the string to set() method. Since, String is an iterable of characters, set() method creates a Set of characters. And since Set holds only unique items, set() returns unique characters present in the given string.

How do I find unique characters in a string in C++?

First we will initialize all values of counter array to 0 and all values of index array to n (length of string). On traversal of the string str and for every character c, increase count[x], if count[x] = 1, index[x] = i. If count[x] = 2, index[x] = n. Sort indexes and print characters.

Does a string have all unique characters?

If the value does not match for all the pairs of characters in a string, then the string has all unique characters.


2 Answers

The simplest solution is probably:

In [10]: ''.join(set('aaabcabccd')) Out[10]: 'acbd' 

Note that this doesn't guarantee the order in which the letters appear in the output, even though the example might suggest otherwise.

You refer to the output as a "list". If a list is what you really want, replace ''.join with list:

In [1]: list(set('aaabcabccd')) Out[1]: ['a', 'c', 'b', 'd'] 

As far as performance goes, worrying about it at this stage sounds like premature optimization.

like image 137
NPE Avatar answered Oct 01 '22 20:10

NPE


Use an OrderedDict. This will ensure that the order is preserved

>>> ''.join(OrderedDict.fromkeys( "aaabcabccd").keys()) 'abcd' 

PS: I just timed both the OrderedDict and Set solution, and the later is faster. If order does not matter, set should be the natural solution, if Order Matter;s this is how you should do.

>>> from timeit import Timer >>> t1 = Timer(stmt=stmt1, setup="from __main__ import data, OrderedDict") >>> t2 = Timer(stmt=stmt2, setup="from __main__ import data") >>> t1.timeit(number=1000) 1.2893918431815337 >>> t2.timeit(number=1000) 0.0632140599081196 
like image 45
Abhijit Avatar answered Oct 01 '22 20:10

Abhijit