ScalaWeekly - find the longest pair of anagrams in a list of strings Subscribe Pub

Given a list or array of strings of varying lengths, find the anagrams in it (exact same characters but in a different sequence) and print out the longest anagram's size.

val l = Array("1234", "123", "321", "5678", "4321")

// to print "4" or such

My solution is below: refrain from looking at it and try to think of your own.

Then: can you find a better one (more optimal)? That's not so hard, really, I left some fat here and there...

val l = Array("1234", "123", "321", "5678", "4321")

// turn them into a code: strings sorted alphabetically of equal length will have same hashcode
def fcode(s:String) = s.toArray.sortWith (_ > _).mkString.hashCode

// sort by length
val s = l.sortWith(_.length > _.length)

// sort by unique code
val m = s.map(fcode).sortWith(_ > _)

//now the anamagrams are next to eachother
(0 until m.size-1).find(i=> m(i) == m(i+1)).map(j=>s(j).length)

Try it now: bit.ly/1phF0Dp.

Not the most efficient, but straitforward...?

Homework

This is a brain de-musher, so here's some things to look at.

Because this is a functional lazy approach, it will in fact not execute much until the final loop. As the comparisons start in length order, they will be calculated on the fly until the equality and no more.... all this, only if the Array and sortWith/map are lazy... homework...

Even if the strings are sorted - does that guarantee that for the same length, the hashcode will be enough or do we need to, as usual, compare the strings as well - and how would you reorganize the code to allow that.

Do you need a memory-saving or a CPU-saving algorithm? How would you change it for that?

How to avoid allocating so many objects?

How lazy can a sort be?


Was this useful?    

By: Razie | 2014-06-10 .. 2016-05-11 | Tags: post , scala , weekly


See more in: Cool Scala Subscribe

Viewed 832 times ( | History | Print ) this page.

You need to log in to post a comment!