Algorithms : Given two strings , write a method to decide if one is permutation of other
// C# program to check whether two strings are permutations of each other
using System;
class AllTechnicalFAQs
{
/* function to check whether two strings are permutation of each other */
public bool isPermutation(String str1, String str2)
{
// Get lenghts of both strings
int n1 = str1.Length;
int n2 = str2.Length;
// If length of both strings is not same,
// then they cannot be Permutation
if (n1 != n2)
return false;
char [] ch1 = str1.ToCharArray();
char [] ch2 = str2.ToCharArray();
// Sort both strings
Array.Sort(ch1);
Array.Sort(ch2);
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (ch1[i] != ch2[i])
return false;
return true;
}
/* Main Block*/
public static void Main(String[] args)
{
String str1 = Console.Readline();
String str2 = Console.Readline();
if (isPermutation(str1, str2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code contributed by Rajput-Ji
No comments:
Post a Comment