An anagram is a rearrangement of the letters of one word or phrase to another word or phrase, using all the original letters exactly once.
Write a function to check whether two given strings are anagram of each other or not. For example, abcd
and dabc
are anagram of each other.
Kotlin program to strings are anagram of each other or not
import java.util.*
fun main(args: Array) {
var str1 = "mother in law";
var str2 = "women hitler";
var isAnagram = Arrays.equals(str1.chars().sorted().toArray(),
str2.chars().sorted().toArray());
if (isAnagram)
System.out.println("Strings are Anagrams of each other");
else
System.out.printf("Strings are not Anagrams of each other");
}
Output
Strings are Anagrams of each other
import java.util.HashMap
fun main(args: Array<String>) {
val three = "dorm"
val four = "mordor"
//LOTR reference
println(areAnagrams(three, four))
val five = "tom marvolo riddle"
val six = "iam lord voldemort"
//the famous Harry Potter reference
println(areAnagrams(five, six))
}
private fun areAnagrams(one: String, two: String): Boolean {
val map = HashMap<Char, Int>()
for (c in one.toCharArray())
if (map.containsKey(c))
map[c] = map[c]!! + 1
else
map[c] = 1
for (c in two.toCharArray())
if (!map.containsKey(c))
return false
else {
map[c] = map[c]!! - 1
if (map[c] == 0)
map.remove(c)
}
return map.isEmpty()
}
Output of program
false
true
Write a program to find the given number is Armstrong number or not
Check whether two strings are anagram of each other
Write a kotlin program to find common elements between two arrays
Write a program to find out duplicate characters in a string
Write a kotlin Program to find Factorial of a Given Number
Write a kotlin program to reverse a string using recursive methods
Write a program to check the given number is a prime number or not
Check if two given strings are isomorphic to each other
Kth Largest Element in an array with kotlin
Write a kotlin program to find perfect number or not
A Program to check if strings are rotations of each other or not
Write a Kotlin Program to Remove Common Characters From Given Strings
Kotlin program to reverse a given number
Write a kotlin program to reverse a string using recursive methods
Write a kotlin program to reverse a string using recursive methods
Reverse Words in a String using kotlin
How to swap two numbers without using temporary variable
Write a program to find top two maximum numbers in a array
| |||||
Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.