Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace SubstringSO
- {
- class Program
- {
- static void Main(string[] args)
- {
- String input = "star, artifact, book, ctenophore, list, reply";
- String pattern = "[a-zA-Z]+";
- var words = Regex.Matches(input, pattern).Cast<Match>().Select(m => m.Value).Where(r => r.Length >= 2);
- Dictionary<string, string> lookup = new Dictionary<string, string>();
- string maxString = "";
- foreach (var word in words)
- {
- string initial = word.Substring(0, 2);
- string end = word.Substring(word.Length - 2, 2);
- if (!lookup.ContainsKey(initial))
- {
- if (!lookup.ContainsKey(end))
- {
- lookup.Add(end, word);
- }
- else if (lookup[end].Length < word.Length)
- {
- lookup[end] = word;
- }
- if (word.Length > maxString.Length)
- maxString = word;
- }
- else
- {
- string newString = lookup[initial] + ", " + word;
- if (!lookup.ContainsKey(end))
- {
- lookup.Add(end, newString);
- }
- else if (lookup[end].Length < newString.Length)
- {
- lookup[end] = newString;
- }
- if (newString.Length > maxString.Length)
- maxString = newString;
- }
- }
- Console.WriteLine(maxString);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement