How to get the number of characters that two strings have in common in Scala

1 Answer

0 votes
def commonCharactersCount(str1: String, str2: String): Int = {
  str1.toSet.intersect(str2.toSet).size
}

val str1 = "abcdefg"
val str2 = "xayzgoe"

println(commonCharactersCount(str1, str2))

  
     
/*
run:
  
3
 
*/

 



answered Mar 20, 2025 by avibootz
...