Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `ABCDE` // does not contain repeats
- `AABCD` // does contain repeats, ie A is repeated
- int longestRun =
- s.Select((c, i) => s.Substring(i).TakeWhile(x => x == c).Count()).Max();
- bool containsDups = "ABCDEA".Length != s.Distinct().Count();
- var results = stringInput
- .ToCharArray() // not actually needed, I've left it here to show what's actually happening
- .GroupBy(c=>c)
- .Where(g=>g.Count()>1)
- .Select(g=>new {Letter=g.First(),Count=g.Count()})
- ;
- public static bool IsSortedNoRepeats(string text)
- {
- if (text.Length == 0)
- {
- return true;
- }
- char current = text[0];
- for (int i=1; i < text.Length; i++)
- {
- char next = text[i];
- if (next <= current)
- {
- return false;
- }
- current = next;
- }
- return true;
- }
- public static bool IsSortedNoRepeats(string text)
- {
- for (int i=1; i < text.Length; i++)
- {
- if (text[i] <= text[i-1])
- {
- return false;
- }
- }
- return true;
- }
- public static int LongestRun(string text)
- {
- if (text.Length == 0)
- {
- return 0;
- }
- char current = text[0];
- int currentRun = 1;
- int bestRun = 0;
- for (int i=1; i < text.Length; i++)
- {
- if (current != text[i])
- {
- bestRun = Math.Max(currentRun, bestRun);
- currentRun = 0;
- current = text[i];
- }
- currentRun++;
- }
- // It's possible that the final run is the best one
- return Math.Max(currentRun, bestRun);
- }
- public static int LongestRun(this IEnumerable<T> source)
- {
- bool first = true;
- T current = default(T);
- int currentRun = 0;
- int bestRun = 0;
- foreach (T element in source)
- {
- if (first || !EqualityComparer<T>.Default(element, current))
- {
- first = false;
- bestRun = Math.Max(currentRun, bestRun);
- currentRun = 0;
- current = element;
- }
- }
- // It's possible that the final run is the best one
- return Math.Max(currentRun, bestRun);
- }
- bool foundMatch = false;
- foundMatch = Regex.IsMatch(yourString, @"(w)1");
- Match match = null;
- string testString = "ABCDE AABCD";
- match = Regex.Match(testString, @"(w)1+?");
- if (match.Success)
- {
- string matchText = match.Value; // AA
- int matchIndnex = match.Index; // 6
- int matchLength = match.Length; // 2
- }
- string strString = "AA BRA KA DABRA";
- var grp = from c in strString.ToCharArray()
- group c by c into m
- select new { Key = m.Key, Count = m.Count() };
- foreach (var item in grp)
- {
- Console.WriteLine(
- string.Format("Character:{0} Appears {1} times",
- item.Key.ToString(), item.Count));
- }
- /(.).*1/
- String input = "AABCD";
- var result = new Dictionary<Char, int>(26);
- var chars = input.ToCharArray();
- foreach (var c in chars)
- {
- if (!result.ContainsKey(c))
- {
- result[c] = 0; // initialize the counter in the result
- }
- result[c]++;
- }
- foreach (var charCombo in result)
- {
- Console.WriteLine("{0}: {1}",charCombo.Key, charCombo.Value);
- }
Add Comment
Please, Sign In to add comment