-
Notifications
You must be signed in to change notification settings - Fork 55
/
77.StringCompress.cs
101 lines (92 loc) · 3 KB
/
77.StringCompress.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//Write a string compress function: give you "RRGB" and return "R2G1B1"
//CC150 1.5
//solution:
//use a StringBuilder to build the result string
//use a char last to keep track of the last found char in the string
//use an int count to store how many same concecutive char
//traverse from the second char of string, if current char == last, count++
//else: StringBuilder.append(current char), StringBuilder.append(count)
//after the loop, append the current char and count to StringBuilder
//If cannot use StringBuilder, use a char array instead
//Note that count is possibely more than two digits, so must convert
//count to char array, then insert each digit to the result char array
//use an int index to keep track of the current insertion location of the char array
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace StringCompress
{
public class Finder{
public static string Compress(string str){
if (str == null)
return null;
StringBuilder sb = new StringBuilder ();
char last = str [0];
int count = 1;
for (int i = 1; i < str.Length; i++) {
if (str [i] == last) {
count++;
} else {
sb.Append (last);
sb.Append (count);
last = str [i];
count = 1; //do not forget reset count
}
}
sb.Append (last);//append last result
sb.Append (count);
string result = sb.ToString ();
return result;
}
public static string CompressNoStringBuilder(string str){
if (str == null)
return null;
int length = str.Length;
char[] temp = new char[length*2];//result string is at most twice as long as the input string
char last = str [0];
int index = 0;
int count = 1;
for (int i = 1; i < length; i++) {
if (str [i] == last) {
count++;
} else {
index = SetChar (temp, last, index, count);
count=1;
last = str [i];
}
}
index = SetChar (temp, last, index, count);//append last result
string result = new string (temp);
return result;
}
//put current char and count correctly to char array
private static int SetChar(char[] arr, char c, int index, int count){
arr [index] = c;
index++;
//it's possible the count has more than one digit, so must convert it to a char array
//instead of a single char
char[] cnt = count.ToString ().ToCharArray (); //convert int to a char array
foreach(char ch in cnt){
arr [index] = ch;
index++;
}
return index;
}
}
class MainClass
{
public static void Main (string[] args)
{
string test = "RRGB";
Console.WriteLine (Finder.Compress(test));
Console.WriteLine (Finder.Compress("aaaaaaaaaaaaaaaaaaaaaaadasdsadasdf"));
Console.WriteLine (Finder.Compress("a"));
Console.WriteLine (Finder.Compress("abcd"));
Console.WriteLine (Finder.CompressNoStringBuilder(test));
Console.WriteLine (Finder.CompressNoStringBuilder("aaaaaaaaaaaaaaaaaaaaaaadasdsadasdf"));
Console.WriteLine (Finder.CompressNoStringBuilder("a"));
Console.WriteLine (Finder.CompressNoStringBuilder("abcd"));
}
}
}