描述
计算一个句子里"Sand", “Water”, “Fish”, 和 “Sun” 的出现次数,忽略大小写
Beaches are filled with sand, water, fish, and sun. Given a string, calculate how many times the words “Sand”, “Water”, “Fish”, and “Sun” appear without overlapping (regardless of the case).
Examples
SumOfABeach("WAtErSlIde") ==> 1
SumOfABeach("GolDeNSanDyWateRyBeaChSuNN") ==> 3
SumOfABeach("gOfIshsunesunFiSh") ==> 4
SumOfABeach("cItYTowNcARShoW") ==> 0
测试用例
namespace Solution {
using NUnit.Framework;
using System;
using System.Text.RegularExpressions;
[TestFixture]
public class Tests
{
[Test]
public void SampleTests()
{
Assert.AreEqual(1, Kata.SumOfABeach("SanD"));
Assert.AreEqual(1, Kata.SumOfABeach("sunshine"));
Assert.AreEqual(4, Kata.SumOfABeach("sunsunsunsun"));
Assert.AreEqual(1, Kata.SumOfABeach("123FISH321"));
}
[Test]
public void HarderTests()
{
Assert.AreEqual(4, Kata.SumOfABeach("weoqipurpoqwuirpousandiupqwoieurioweuwateruierqpoiweurpouifiShqowieuqpwoeuisUn"));
Assert.AreEqual(10, Kata.SumOfABeach("sAnDsandwaTerwatErfishFishsunsunsandwater"));
Assert.AreEqual(10, Kata.SumOfABeach("joifjepiojfoiejfoajoijawoeifjowejfjoiwaefjiaowefjaofjwoj fawojef jwefjwjfsandsandwaterwaterfishfishsunsunsandwateriojwhefa;jawof;jawio;f"));
Assert.AreEqual(0, Kata.SumOfABeach("oijifwefoiwoijiawijiwefiwoefiowefjiowefjwjafojjfwejfaiwfjaw;fjwfoi;wejfa;wojfawo;jfawo;jf;owejf;aowjf;jowaeofwaf;jowefa;owjf"));
Assert.AreEqual(100, Kata.SumOfABeach("saNdsandwaterwAterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwatersandsandwaterwaterfishfishsunsunsandwater"));
}
[Test]
public void SlicedWords()
{
Assert.AreEqual(1, Kata.SumOfABeach("sununsu"));
Assert.AreEqual(1, Kata.SumOfABeach("sandandndsansa"));
Assert.AreEqual(1, Kata.SumOfABeach("wateratertererwatewatwa"));
Assert.AreEqual(1, Kata.SumOfABeach("fishishshfisfi"));
}
[Test]
public void RandomTests()
{
string[] words = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w", "x", "y", "z", "sand", "water", "fish", "sun",
"sand", "water", "fish", "sun", "sand", "water", "fish", "sun"};
Random random = new Random();
for (int i = 0; i < 100; i++)
{
string result = "";
string randomWord = words[random.Next(0, words.Length)];
while (result.Length < 50)
{
result += randomWord;
randomWord = words[random.Next(0, words.Length)];
randomWord = random.Next(1, 10) >= 4 ? randomWord : randomWord.ToUpper();
}
int matches = 0;
matches += Regex.Matches(result.ToLower(), "sand").Count;
matches += Regex.Matches(result.ToLower(), "water").Count;
matches += Regex.Matches(result.ToLower(), "fish").Count;
matches += Regex.Matches(result.ToLower(), "sun").Count;
Assert.AreEqual(matches, Kata.SumOfABeach(result));
}
}
}
}
解决方案:
正则大法
using System.Text.RegularExpressions;
public class Kata
{
public static int SumOfABeach(string s)
{
return Regex.Matches(s.ToLower(),"water|fish|sand|sun").Count;
// return Regex.Matches(s, "(sand|sun|water|fish)", RegexOptions.IgnoreCase).Count;
// return Matches(s, "(?i)sand|water|fish|sun").Count;
}
}
分割大法
using System;
public class Kata
{
public static int SumOfABeach(string s)
{
return s.ToLower().Split(new string[] { "sand", "water", "fish", "sun" }, StringSplitOptions.None).Length - 1;
}
}
移除大法
using System;
using System.Linq;
public class Kata
{
//Feel my wrath.
public static int SumOfABeach(string s) => s.ToLower().Replace(" ", "").Replace("sand", " ").Replace("water", " ").Replace("fish", " ").Replace("sun", " ").Count(i => i == ' ');
}
遍历统计
public class Kata
{
public static int SumOfABeach(string s)
{
var count = 0;
s = s.ToLower();
for(var i=0;i<s.Length;i++)
{
var sub = s.Substring(i);
if(sub.StartsWith("water")
|| sub.StartsWith("fish")
|| sub.StartsWith("sand")
|| sub.StartsWith("sun"))
count++;
}
return count;
}
}