Implement a method to perform basic string compression using the counts of repeated characters.

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Text;

   5:  

   6: namespace _1_05_CompressionString

   7: {

   8:     class Program

   9:     {

  10:         private static int countCompression(string str)

  11:         {

  12:             int size = 0;

  13:  

  14:             char last = str[0];

  15:             int count = 1;

  16:  

  17:             for (int i = 0; i < str.Length; i++)

  18:             {

  19:                 if (str[i] == last)

  20:                 {

  21:                     count++;

  22:                 }

  23:                 else

  24:                 {

  25:                     last = str[i];

  26:                     size += 1 + count.ToString().Length;

  27:                     count = 1;

  28:                 }

  29:             }

  30:             size += 1 + count.ToString().Length;

  31:             return size;

  32:         }

  33:  

  34:  

  35:         private static string Compression1(string str)

  36:         {

  37:             int size = countCompression(str);

  38:             if (size >= str.Length)

  39:             {

  40:                 return str;

  41:             }

  42:  

  43:             StringBuilder stb = new StringBuilder();

  44:             char last = str[0];

  45:             int count = 1;

  46:  

  47:             for (int i = 1; i < str.Length; i++)

  48:             {

  49:                 if (last == str[i])

  50:                 {

  51:                     count++;

  52:                 }

  53:                 else

  54:                 {

  55:                     stb.Append(last);

  56:                     stb.Append(count);

  57:                     last = str[i];

  58:                     count = 1;

  59:                 }

  60:             }

  61:             stb.Append(last);

  62:             stb.Append(count);

  63:  

  64:             return stb.ToString();

  65:         }

  66:  

  67:         private static string Compression2(string str)

  68:         {

  69:             int size = countCompression(str);

  70:             if (size >= str.Length)

  71:             {

  72:                 return str;

  73:             }

  74:  

  75:             string result = string.Empty;

  76:             char last = str[0];

  77:             int count = 1;

  78:  

  79:             for (int i = 1; i < str.Length; i++)

  80:             {

  81:                 if (last == str[i])

  82:                 {

  83:                     count++;

  84:                 }

  85:                 else

  86:                 {

  87:                     result += last.ToString();

  88:                     result += count.ToString();

  89:                     last = str[i];

  90:                     count = 1;

  91:                 }

  92:             }

  93:             result += last.ToString();

  94:             result += count.ToString();

  95:  

  96:             return result;

  97:         }

  98:  

  99:         static void Main(string[] args)

 100:         {

 101:             string str = "abbcccccccccccccccccccccccccccde";

 102:  

 103:             Console.WriteLine("{0} : {1}", str, countCompression(str)); 

 104:  

 105:             Console.WriteLine("{0} : {1}", str, Compression1(str)); 

 106:             Console.WriteLine("{0} : {1}", str, Compression2(str));

 107:  

 108:             Console.ReadKey();

 109:         }

 110:     }

 111: }

Advertisements