Guest User

Untitled

a guest
Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. `ABCDE` // does not contain repeats
  2. `AABCD` // does contain repeats, ie A is repeated
  3.  
  4. int longestRun =
  5. s.Select((c, i) => s.Substring(i).TakeWhile(x => x == c).Count()).Max();
  6.  
  7. bool containsDups = "ABCDEA".Length != s.Distinct().Count();
  8.  
  9. var results = stringInput
  10. .ToCharArray() // not actually needed, I've left it here to show what's actually happening
  11. .GroupBy(c=>c)
  12. .Where(g=>g.Count()>1)
  13. .Select(g=>new {Letter=g.First(),Count=g.Count()})
  14. ;
  15.  
  16. public static bool IsSortedNoRepeats(string text)
  17. {
  18. if (text.Length == 0)
  19. {
  20. return true;
  21. }
  22. char current = text[0];
  23. for (int i=1; i < text.Length; i++)
  24. {
  25. char next = text[i];
  26. if (next <= current)
  27. {
  28. return false;
  29. }
  30. current = next;
  31. }
  32. return true;
  33. }
  34.  
  35. public static bool IsSortedNoRepeats(string text)
  36. {
  37. for (int i=1; i < text.Length; i++)
  38. {
  39. if (text[i] <= text[i-1])
  40. {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46.  
  47. public static int LongestRun(string text)
  48. {
  49. if (text.Length == 0)
  50. {
  51. return 0;
  52. }
  53. char current = text[0];
  54. int currentRun = 1;
  55. int bestRun = 0;
  56.  
  57. for (int i=1; i < text.Length; i++)
  58. {
  59. if (current != text[i])
  60. {
  61. bestRun = Math.Max(currentRun, bestRun);
  62. currentRun = 0;
  63. current = text[i];
  64. }
  65. currentRun++;
  66. }
  67. // It's possible that the final run is the best one
  68. return Math.Max(currentRun, bestRun);
  69. }
  70.  
  71. public static int LongestRun(this IEnumerable<T> source)
  72. {
  73. bool first = true;
  74. T current = default(T);
  75. int currentRun = 0;
  76. int bestRun = 0;
  77.  
  78. foreach (T element in source)
  79. {
  80. if (first || !EqualityComparer<T>.Default(element, current))
  81. {
  82. first = false;
  83. bestRun = Math.Max(currentRun, bestRun);
  84. currentRun = 0;
  85. current = element;
  86. }
  87. }
  88. // It's possible that the final run is the best one
  89. return Math.Max(currentRun, bestRun);
  90. }
  91.  
  92. bool foundMatch = false;
  93. foundMatch = Regex.IsMatch(yourString, @"(w)1");
  94.  
  95. Match match = null;
  96. string testString = "ABCDE AABCD";
  97. match = Regex.Match(testString, @"(w)1+?");
  98. if (match.Success)
  99. {
  100. string matchText = match.Value; // AA
  101. int matchIndnex = match.Index; // 6
  102. int matchLength = match.Length; // 2
  103. }
  104.  
  105. string strString = "AA BRA KA DABRA";
  106.  
  107. var grp = from c in strString.ToCharArray()
  108. group c by c into m
  109. select new { Key = m.Key, Count = m.Count() };
  110.  
  111. foreach (var item in grp)
  112. {
  113. Console.WriteLine(
  114. string.Format("Character:{0} Appears {1} times",
  115. item.Key.ToString(), item.Count));
  116. }
  117.  
  118. /(.).*1/
  119.  
  120. String input = "AABCD";
  121. var result = new Dictionary<Char, int>(26);
  122. var chars = input.ToCharArray();
  123. foreach (var c in chars)
  124. {
  125. if (!result.ContainsKey(c))
  126. {
  127. result[c] = 0; // initialize the counter in the result
  128. }
  129. result[c]++;
  130. }
  131.  
  132. foreach (var charCombo in result)
  133. {
  134. Console.WriteLine("{0}: {1}",charCombo.Key, charCombo.Value);
  135. }
Add Comment
Please, Sign In to add comment