Check if two strings are permutation of each other
Solutions
Approach – Sort the strings
public bool ArePermutation(string str1, string str2)
{
if (str1 is null && str2 is null)
return true;
if (str1?.Length != str2?.Length)
return false;
var char1 = str1!.ToArray();
var char2 = str2!.ToArray();
Array.Sort(char1);
Array.Sort(char2);
for (int i = 0; i < str1!.Length; i++)
if (char1[i] != char2[i])
return false;
return true;
}Approach – Check if the two strings have identical character counts
Last updated