Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# code to check anagram

string a = "abcd";
string b = "bcda"; // bad daa a1b2c3 abc123

string aa = String.Concat(a.OrderBy(c => c));
string bb = String.Concat(b.OrderBy(c => c));

if (aa == bb)
{
     Console.WriteLine("YES");
}
else
{
     Console.WriteLine("NO");
}
Comment

c# code to check anagram

public bool IsAnagram(string s1,string s2)
{
  if (s1.Length != s2.Length)
     return false;
  var s1Array = s1.ToLower().ToCharArray();
  var s2Array = s2.ToLower().ToCharArray();

  Array.Sort(s1Array);
  Array.Sort(s2Array);

  s1 = new string(s1Array);
  s2 = new string(s2Array);

  return s1 == s2;
}
Comment

c# code to check anagram

void Test()
{
    string a = "abccabccabccabccabccabccabccabccabccabccabccabccabccabccabccabcc";
    string b = "bcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcacbcac";

    IsAnagramSimple(a, b);
    IsAnagramFast(a, b);
}


private bool IsAnagramSimple(string a, string b)
{
    return a.OrderBy(c => c).SequenceEqual(b.OrderBy(c => c));
}

private bool IsAnagramFast(string a, string b)
{
    if (a.Length != b.Length)
    {
        return false;
    }

    var aFrequency = CalculateFrequency(a);
    var bFrequency = CalculateFrequency(b);

    foreach (var key in aFrequency.Keys)
    {
        if (!bFrequency.ContainsKey(key)) return false;
        if (aFrequency[key] != bFrequency[key]) return false;
    }

    return true;
}

private Dictionary<char, int> CalculateFrequency(string input)
{
    var frequency = new Dictionary<char, int>();
    foreach (var c in input)
    {
        if (!frequency.ContainsKey(c))
        {
            frequency.Add(c, 0);
        }
        ++frequency[c];
    }
    return frequency;
}
Comment

c# code to check anagram

if (String.Concat(a.OrderBy(c => c)).Equals(String.Concat(b.OrderBy(c => c))) ...
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to iterate string in c# 
Csharp :: blazor image button 
Csharp :: unity easing out 
Csharp :: wpf get name of clicked element 
Csharp :: or operator in shell 
Csharp :: c# convert excel column index to letter 
Csharp :: begininvoke async c# 
Csharp :: c# networkstream read all bytes 
Csharp :: JavaScriptSerializer() and convert to base64 
Csharp :: Count the Number of Duplicate Characters 
Csharp :: how to count letters in c# 
Csharp :: unity error log 
Csharp :: c# clear panel 
Csharp :: how to check if button is pressed unity 
Csharp :: list with search bar uwp c# 
Csharp :: page parent wpf 
Csharp :: how to get properties from json in c# 
Csharp :: sql server query output to json file automatically 
Csharp :: how to empty an array c# 
Csharp :: by value by reference c# 
Csharp :: implicit vs explicit cast c# 
Csharp :: *ngif vs ngif 
Csharp :: how to lerp a value in unity 
Csharp :: c# get enum name from value 
Csharp :: blazor use static json files 
Csharp :: How to build a rest component with very long process 
Csharp :: converting dens_rank and row_number to linq 
Csharp :: call class c# 
Csharp :: c# accept any enum 
Csharp :: Set orientation of moving object towards it movement direction without using rigidbody 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =