1

In Python

s= "ABCC"
n = len(s)
sorted(set([s[a:b] for a in range(n) for b in range(a+1,n+2)])

gives me, alphabetically sorted sub strings with out repetitions

['A', 'AB', 'ABC', 'ABCC', 'B', 'BC', 'BCC', 'C', 'CC']

How can I further sort it by length of sub string.

['A', 'B', 'C', 'AB', 'BC', 'CC', 'ABC', 'BCC', 'ABCC']
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Kamath
  • 4,461
  • 5
  • 33
  • 60

2 Answers2

3

simple,

sorted(set(s[a:b] for a in range(n) for b in range(a+1,n+1)),
       key=lambda x:(len(x),x))

This creates a key by which the comparison is done. First it compares the string lengths to determine the order. If the strings have the same length, the tie-breaker is the string contents.

georg
  • 211,518
  • 52
  • 313
  • 390
mgilson
  • 300,191
  • 65
  • 633
  • 696
2

This is your solution:

s= "ABCC"
n = len(s)
sorted(sorted(set([s[a:b] for a in range(n) for b in range(a+1,n+2)])),key=len)
Bart
  • 19,692
  • 7
  • 68
  • 77
Guddu
  • 1,588
  • 4
  • 25
  • 53
  • And how is that solution wrong? Did you run it yourself? Please run it in Python and tell me if it is wrong or not. – Guddu Feb 05 '13 at 15:30
  • Python sorting is guaranteed to be stable, so there's nothing wrong about double `sorted`. Upvoted. – georg Feb 05 '13 at 15:33
  • I missed the fact that you're using two `sorted` calls(ignore my last comment(deleted)), +1. – Ashwini Chaudhary Feb 05 '13 at 15:37
  • mgilson....Could you prove your point? I have tested this with this string also s= "NOPQRSTUVWXYZABCDEFGHIJKLM" – Guddu Feb 05 '13 at 15:41
  • I am wondering why two times sorting is required, 'set' sorts the list alphabetically, then you sort the with sorted for len. – Kamath Feb 05 '13 at 16:12
  • @AUZKamath set does not do any sorting. It only constructs an unordered collection. Then the first sorted sorts alphabetically and the second one sorts that alphabetically sorted collection by length. – Guddu Feb 05 '13 at 16:16