fork(10) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static IEnumerable<string> getAllSubstrings(string word)
  8. {
  9. return from charIndex1 in Enumerable.Range(0, word.Length)
  10. from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
  11. where charIndex2 >= 2
  12. select word.Substring(charIndex1, charIndex2);
  13. }
  14.  
  15. public static void Main()
  16. {
  17. var list = new List<string>() {
  18. "ToCommondays","MonCommonday_","TuesCommonda","WednesCommon_day"
  19. };
  20.  
  21. string shortest = list.OrderBy(s => s.Length).First();
  22. IEnumerable<string> shortestSubstrings = getAllSubstrings(shortest)
  23. .OrderByDescending(s => s.Length);
  24. var other = list.Where(s => s != shortest).ToArray();
  25. string longestCommonIntersection = string.Empty;
  26. foreach (string subStr in shortestSubstrings)
  27. {
  28. bool allContains = other.All(s => s.Contains(subStr));
  29. if (allContains)
  30. {
  31. longestCommonIntersection = subStr;
  32. break;
  33. }
  34. }
  35. Console.Write("longestCommonIntersection=" + longestCommonIntersection);
  36. }
  37. }
Success #stdin #stdout 0.05s 37296KB
stdin
Standard input is empty
stdout
longestCommonIntersection=Common