Advertisement
TheRedRover

Untitled

Apr 10th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.70 KB | None | 0 0
  1.  
  2.   def duplicateEncode(word: String) = {
  3.     def helper(str:String,i:Int=0,ans:String=""):String=
  4.       {
  5.         if(str.toLowerCase.count(_==str.toLowerCase.charAt(i))>1)
  6.           {
  7.             val strN = ans+")"
  8.             if(i<str.length-1)
  9.               helper(str, i+1, strN)
  10.             else strN
  11.           }
  12.         else {
  13.           val strN = ans+"("
  14.           if(i<str.length-1)
  15.             helper(str, i+1, strN)
  16.           else strN
  17.         }
  18.       }
  19.     helper(word)
  20.   }
  21.  
  22. /////////////////////////////////////////////////
  23. object Solution {
  24.   def duplicateEncode(word: String) = {
  25.     word.toLowerCase.map(c => if (word.toLowerCase.count(_ == c) <= 1) "(" else ")").mkString
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement