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.1.180 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/anagram/Example.cs