Kotlin Program to Check if Two Strings are Anagrams

In this blog post, we will learn how to write a Kotlin program to check if two Strings are Anagrams.

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. In programming, checking if two strings are anagram is a common problem. 

In this blog post, we will explore a Kotlin program that efficiently determines whether two strings are anagrams of each other. We will walk through the program step by step, explaining the logic behind it.

Kotlin Program: Checking if Two Strings are Anagrams

To check if two strings are anagrams in Kotlin, we can follow a straightforward approach. Let's dive into the code:
fun areAnagrams(str1: String, str2: String): Boolean {
    if (str1.length != str2.length) {
        return false
    }

    val charCount = IntArray(26)

    for (i in str1.indices) {
        charCount[str1[i] - 'a']++
        charCount[str2[i] - 'a']--
    }

    for (count in charCount) {
        if (count != 0) {
            return false
        }
    }

    return true
}

fun main() {
    val str1 = "listen"
    val str2 = "silent"
    val areAnagrams = areAnagrams(str1, str2)

    if (areAnagrams) {
        println("$str1 and $str2 are anagrams.")
    } else {
        println("$str1 and $str2 are not anagrams.")
    }
}

Output:

listen and silent are anagrams.
Explanation: 
The areAnagrams() function takes two strings, str1, and str2, as input and returns a Boolean value indicating whether they are anagrams. 

First, we check if the lengths of str1 and str2 are equal. If they are not, the strings cannot be anagrams, so we immediately return false. 

We create an IntArray named charCount of size 26 to store the count of each character. The index of the array represents the character's position in the alphabet. 

Using a for loop, we iterate over the characters of str1 and str2 simultaneously using the indices property. We increment the count for the corresponding character in str1 and decrement the count for the corresponding character in str2 in the charCount array. 

After the loop, we iterate over the charCount array. If any count is non-zero, it means the frequencies of characters in str1 and str2 are different, and the strings cannot be anagrams. In such cases, we return false.

If all the counts are zero, we conclude that str1 and str2 are anagrams, and we return true. 

In the main() function, we create two sample input strings (val str1 = "listen" and val str2 = "silent") and call the areAnagrams() function with these strings. The function's result is stored in the areAnagrams variable, and we use an if-else statement to print whether the strings are anagrams or not. 

Conclusion

In this blog post, we have discussed a Kotlin program that efficiently checks if two strings are anagrams. By comparing the character frequencies in both strings using an array, we can determine their anagram status. Understanding this program equips you with the necessary skills to handle string manipulation and solve similar anagram-related problems in Kotlin. 

Feel free to explore and modify the code to suit your specific needs. The ability to check for anagrams is valuable in various programming scenarios, including string processing, word games, and cryptography.

Happy coding!

Comments