Sha256: e9ba0bf2ff528aabc58a193105e5e38054f9404412f30448791c37b0257d1866

Contents?: true

Size: 1.13 KB

Versions: 208

Compression:

Stored size: 1.13 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[] Match (string[] potentialMatches)
    {
        List<string> matches = new List<string>();

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

        return matches.OrderBy(word => word).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

208 entries across 208 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.52 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.51 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.50 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.49 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.48 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.47 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.46 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.45 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.44 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.43 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.42 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.41 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.40 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.39 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.38 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.37 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.36 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.34 tracks/csharp/exercises/anagram/Example.cs
trackler-2.1.0.33 tracks/csharp/exercises/anagram/Example.cs