Sha256: 19c3471f19fb3806507c0865ae942a5a270ad463d95369ba3040b06921089c50

Contents?: true

Size: 1.11 KB

Versions: 188

Compression:

Stored size: 1.11 KB

Contents

using System;
using System.Linq;
using System.Collections.Generic;

public class Anagram
{
    private string baseWord;

    public Anagram(string baseWord)
    {
        this.baseWord = baseWord;
    }

    public string[] Anagrams(string[] potentialMatches)
    {
        List<string> matches = new List<string>();

        foreach (string word in potentialMatches)
        {
            if (IsWordAnagramOfBaseWord(word))
            {
                matches.Add(word);
            }
        }

        return matches.ToArray();
    }

    private bool IsWordAnagramOfBaseWord(string word)
    {
        return (IsNotTheSameWordAsBaseWord(word) && HasSameLettersAsBaseWord(word));
    }

    private bool IsNotTheSameWordAsBaseWord(string word)
    {
        return ! baseWord.Equals(word,StringComparison.OrdinalIgnoreCase);
    }

    private bool HasSameLettersAsBaseWord(string word)
    {
        return SortedCharArrayForString(baseWord).Equals(SortedCharArrayForString(word));
    }

    private string SortedCharArrayForString (string word)
    {
        return String.Concat(word.ToLower().OrderBy(letter => letter));
    }
}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.0.5 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.0.4 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.0.3 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.0.2 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.0.1 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.0.0 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.55 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.54 tracks/csharp/exercises/anagram/Example.cs