Quick actions

cmd+k|ctrl+k

Navigation

Languages

Test Anagram

Snippet info

Language

Csharp

Visibility

public

Author

patrykstachowiak

Created

2025-03-11T10:44:56.462611Z

Updated

2025-03-11T10:44:56.462611Z

// Entry point name must be "Solution"
using System;
using System.Collections.Generic;

public static class Solution
{
    private static void Main()
    {
        Console.WriteLine("Hello, world!");
        
        if (AreStringsAnagrams("kayak", "kaaky"))
        {
            Console.WriteLine("Strings are anagrams");
        }
    }
    
    private static bool AreStringsAnagrams(string str1, string str2)
    {
        if (str1.Length != str2.Length) return false;
        
        Dictionary<char, int> charsCount = new Dictionary<char, int>();
        
        foreach (char singleCharacter in str1)
        {
            if (charsCount.ContainsKey(singleCharacter))
            {
                charsCount[singleCharacter]++;
            } else {
                charsCount[singleCharacter] = 1;
            }
        }
        
        foreach (char singleCharacter in str2)
        {
            if (!charsCount.ContainsKey(singleCharacter))
            {
                return false;
            } else {
                charsCount[singleCharacter]--;
            }
        }
        
        return true;
    }
}
INFO