using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Set42_Counting_words
public static void Main(string[] args)
string streamOfCharacters = "acacabcatghhellomvnsdb";
List<string> listOfWords = new List<string>()
"aca","cat","hello","world"
findWithRegExp(streamOfCharacters, listOfWords);
Console.WriteLine("----------------------------------------------------");
findWithBruteForce(streamOfCharacters, listOfWords);
static void findWithRegExp(string streamOfCharacters, List<string> listOfWords) {
foreach (string word in listOfWords)
Console.WriteLine("{0} : {1}", word, Regex.Matches(streamOfCharacters, "(?=(" + word + ")).").Count);
static void findWithBruteForce(string streamOfCharacters, List<string> listOfWords) {
Dictionary<string, int> dictOfWords = new Dictionary<string, int>();
foreach (string word in listOfWords)
if (!dictOfWords.ContainsKey(word))
dictOfWords.Add(word, 0);
for (int i = 0; i < streamOfCharacters.Length - 1; i++)
foreach (string word in listOfWords)
if (streamOfCharacters[i] == word[0])
if (checkWordIncluding(streamOfCharacters, i, word))
foreach (KeyValuePair<string, int> wordWithStat in dictOfWords)
Console.WriteLine("{0} : {1}", wordWithStat.Key, wordWithStat.Value);
static bool checkWordIncluding(string streamOfCharacters, int startPos, string checkingWord)
for (int i = startPos; i < streamOfCharacters.Length-1; i++)
if (j == checkingWord.Length - 1)
if (streamOfCharacters[i] == checkingWord[j])
if (streamOfCharacters[i] != checkingWord[j])