ivandrofly

Parsing string (count char) (codingame problem

Mar 3rd, 2021 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1.             string s = "aaaabbbddddaaa";
  2.  
  3.             //s = "abc";
  4.  
  5.             string output = "";
  6.             char c = s[0];
  7.             int count = 0;
  8.             for (int i = 0; i < s.Length; i++)
  9.             {
  10.                 if (c == s[i])
  11.                 {
  12.                     count++;
  13.                 }
  14.                 else
  15.                 {
  16.                     output += count.ToString() + c;
  17.                     c = s[i];
  18.                     count = 1;
  19.                 }
  20.             }
  21.  
  22.             // include last char
  23.             if (output.Length > 0 && output[output.Length - 1] != c)
  24.             {
  25.                 output += count.ToString() + c;
  26.             }
  27.  
  28.             Console.WriteLine(output); // output: 4a3b4d3a
Add Comment
Please, Sign In to add comment