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.159 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.158 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.157 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.156 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.155 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.154 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.153 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.152 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.151 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.150 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.149 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.148 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.147 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.146 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.145 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.144 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.143 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.142 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.141 tracks/csharp/exercises/anagram/Example.cs
trackler-2.2.1.140 tracks/csharp/exercises/anagram/Example.cs