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

1 Answer

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

fun main() {
    val str1 = "abcdefg"
    val str2 = "xayzgoe"

    println(commonCharactersCount(str1, str2))
}

  
     
/*
run:
  
3
 
*/

 



answered Mar 20, 2025 by avibootz
...