using System.Collections.Generic;
using System.Text.RegularExpressions;
static bool IsDanger1(string body, string dictionary, char separator)
var dict = new Dictionary<string,bool>();
for(int i=0; i<dictionary.Length; i++) {
if (dictionary[i] == separator) {
var key = dictionary.Substring(last, i-last);
if (last < dictionary.Length) {
var key = dictionary.Substring(last);
for(int i=0; i<body.Length; i++) {
var key = body.Substring(last, i-last);
if (dict.ContainsKey(key)) {
if (last < body.Length) {
var key = body.Substring(last);
if (dict.ContainsKey(key)) {
static bool IsDanger2(string body, string dictionary, char separator)
var pattern = dictionary.Replace(separator, '|');
pattern = "\\b(" + pattern + ")\\b";
var re = new Regex(pattern);
static bool IsDanger3(string body, string dictionary, char separator)
var words = dictionary.Split(',');
for(int i=0; i<body.Length; i++) {
word = body.Substring(i-wordLength, wordLength);
if (words.Any(o => o == word)) {
word = body.Substring(body.Length-wordLength, wordLength);
if (words.Any(o => o == word)) {
public Letter(char c, bool isEnd)
static bool IsDanger4(string body, string dictionary, char separator)
var places = new List<List<Letter>>();
for(int i=0; i<dictionary.Length; i++) {
if (places.Count() <= n) {
places.Add( new List<Letter>() );
if (!places[n].Any(o => o.C == c)) {
var isEnd = i == dictionary.Length-1 || dictionary[i+1] == separator;
var letter = new Letter(c, isEnd);
for(int i=0; i<body.Length; i++) {
if (exists || wordLength == 0) {
exists = places.Count() > wordLength;
var isEnd = i == body.Length-1 || !char.IsLetter(body[i+1]);
exists = places[wordLength].Any(o => o.C == c && o.IsEnd == isEnd);
public static void Main()
string body1 = "So I want to start by offering you a free no-tech life hacks1";
string body2 = "So I want to start by offering you a free no-tech life hack";
string dictionary = "test,t,tos,tosp,lif,hacks";
bool b1 = IsDanger1(body1, dictionary, separator);
bool b2 = IsDanger1(body2, dictionary, separator);
Console.WriteLine("{0} : {1} : {2}", "IsDanger1", b1, b2);
b1 = IsDanger2(body1, dictionary, separator);
b2 = IsDanger2(body2, dictionary, separator);
Console.WriteLine("{0} : {1} : {2}", "IsDanger2", b1, b2);
b1 = IsDanger3(body1, dictionary, separator);
b2 = IsDanger3(body2, dictionary, separator);
Console.WriteLine("{0} : {1} : {2}", "IsDanger3", b1, b2);
b1 = IsDanger4(body1, dictionary, separator);
b2 = IsDanger4(body2, dictionary, separator);
Console.WriteLine("{0} : {1} : {2}", "IsDanger4", b1, b2);