Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The problem is: Given two strings, write a method to decide if one is a permutation of the other. I wrote the following code in scala, may I know any other optimization one?

def checkpermutation(str1:String, str2:String): Boolean=(str1, str2) match {
   case (a,b) if a==b => true
   case (a,b) if a.length() !=b.length() =>false
   case (a,b) if a.toList.sorted.mkString== a.toList.sorted.mkString => true
   case _ =>false
 }
share|improve this question
    
The third case is invalid: it checks a.toList.sorted.mkString with itself. – Antot Nov 30 '16 at 12:43
up vote 2 down vote accepted

There is no need to use matchers for this check. A logical expression would be enough:

def checkpermutation2(a:String, b:String): Boolean = {
  def sorted(s: String) = s.sorted.mkString
  (a == b) || (a.length == b.length && sorted(a) == sorted(b))
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.