using System; using System.Linq; public class Kata { public static int DuplicateCount(string s) => s.ToLower() .GroupBy(c => c) .Count(g => g.Skip(1).Any()); }
- using System;
- using System.Linq;
- public class Kata
- {
public static int DuplicateCount(string str){return str.ToLower().GroupBy(c => c).Count (g => g.Count () > 1);}- public static int DuplicateCount(string s) =>
- s.ToLower()
- .GroupBy(c => c)
- .Count(g => g.Skip(1).Any());
- }
using NUnit.Framework; public class DuplicateCount { [Test] public void ShouldReturnZeroWhenEmptyString() => Assert.That(Kata.DuplicateCount(""), Is.EqualTo(0)); [TestCase("a", 0)] [TestCase("ab", 0)] [TestCase("abcde", 0)] [TestCase("ABC", 0)] public void ShouldReturnZeroWhenNoDuplicates(string s, int expected) => Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected)); [TestCase("aa", 1)] [TestCase("bb", 1)] [TestCase("abb", 1)] [TestCase("AAB", 1)] public void ShouldHandleSingleDuplicateCharacter(string s, int expected) => Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected)); [TestCase("aabb", 2)] [TestCase("aabbcde", 2)] [TestCase("abbcdde", 2)] [TestCase("AABCC", 2)] public void ShouldHandleMultipleDuplicates(string s, int expected) => Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected)); [TestCase("aba", 1)] [TestCase("abcb", 1)] [TestCase("abab", 2)] [TestCase("ABCABC", 3)] public void ShouldHandleNonAdjacentCharacters(string s, int expected) => Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected)); [TestCase("aA", 1)] [TestCase("aABb", 2)] [TestCase("aabBcde", 2)] [TestCase("aBcdE", 0)] public void ShouldIgnoreCase(string s, int expected) => Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected)); [Test] public void ShouldCountSingleDuplicateOnce() => Assert.That(Kata.DuplicateCount("Indivisibility"), Is.EqualTo(1), "Indivisibility"); [Test] public void ShouldCountMultipleDuplicatesOncePerCharacter() => Assert.That(Kata.DuplicateCount("Indivisibilities"), Is.EqualTo(2), "Indivisibilities"); }
namespace Solution {using System;using System.Text.RegularExpressions;using System.Collections.Generic;using System.Linq;using NUnit.Framework;[TestFixture]public class KataTest{- using NUnit.Framework;
- public class DuplicateCount
- {
- [Test]
- public void ShouldReturnZeroWhenEmptyString() =>
- Assert.That(Kata.DuplicateCount(""), Is.EqualTo(0));
- [TestCase("a", 0)]
- [TestCase("ab", 0)]
- [TestCase("abcde", 0)]
- [TestCase("ABC", 0)]
- public void ShouldReturnZeroWhenNoDuplicates(string s, int expected) =>
- Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected));
- [TestCase("aa", 1)]
- [TestCase("bb", 1)]
- [TestCase("abb", 1)]
- [TestCase("AAB", 1)]
- public void ShouldHandleSingleDuplicateCharacter(string s, int expected) =>
- Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected));
- [TestCase("aabb", 2)]
- [TestCase("aabbcde", 2)]
- [TestCase("abbcdde", 2)]
- [TestCase("AABCC", 2)]
- public void ShouldHandleMultipleDuplicates(string s, int expected) =>
- Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected));
- [TestCase("aba", 1)]
- [TestCase("abcb", 1)]
- [TestCase("abab", 2)]
- [TestCase("ABCABC", 3)]
- public void ShouldHandleNonAdjacentCharacters(string s, int expected) =>
- Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected));
- [TestCase("aA", 1)]
- [TestCase("aABb", 2)]
- [TestCase("aabBcde", 2)]
- [TestCase("aBcdE", 0)]
- public void ShouldIgnoreCase(string s, int expected) =>
- Assert.That(Kata.DuplicateCount(s), Is.EqualTo(expected));
- [Test]
- public void ShouldCountSingleDuplicateOnce() =>
- Assert.That(Kata.DuplicateCount("Indivisibility"), Is.EqualTo(1), "Indivisibility");
- [Test]
public void KataTests(){Assert.AreEqual(0, Kata.DuplicateCount(""));Assert.AreEqual(0, Kata.DuplicateCount("abcde"));Assert.AreEqual(2, Kata.DuplicateCount("aabbcde"));Assert.AreEqual(0, Kata.DuplicateCount("a"));Assert.AreEqual(1, Kata.DuplicateCount("aa"));Assert.AreEqual(2, Kata.DuplicateCount("aabBcde"), "should ignore case");Assert.AreEqual(1, Kata.DuplicateCount("Indivisibility"));Assert.AreEqual(2, Kata.DuplicateCount("Indivisibilities"), "characters may not be adjacent");}}- public void ShouldCountMultipleDuplicatesOncePerCharacter() =>
- Assert.That(Kata.DuplicateCount("Indivisibilities"), Is.EqualTo(2), "Indivisibilities");
- }