-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-CollapseConsecutiveCharacters.cs
57 lines (46 loc) · 1.43 KB
/
05-CollapseConsecutiveCharacters.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
using System;
using System.Collections.Generic;
using System.Text;
namespace HackerRank
{
public class CollapseConsecutiveCharacters_Result
{
public static string CollapseCharacters(string str)
{
string result = "";
int range = str.Length;
for (int i = 0; i < range; i++)
{
int count = 1;
//comparision
while(i < range-1 && str[i] == str[i + 1])
{
count++;
i++;
}
result = count > 1 ? $"{result}{str[i]}{count}" : $"{result}{str[i]}";
}
return result;
}
}
public class CollapseConsecutiveCharacters_Solution
{
public static void CharactersCount()
{
//Test Case-1
//string Input = "aaabccccdde";
//Output : a3bc4d2e
//Test Case-2
//string Input = "yyyyyzzzzzzzzzz";
//Output: y5z10
//Test Case-3
//string Input = "abcd";
//Output: abcd(note: not a1b1c1d1)
//Test Case-4
string Input ="hhhccchhhccc";
//Output: h3c3h3c3
string OutPut = CollapseConsecutiveCharacters_Result.CollapseCharacters(Input);
Console.WriteLine(OutPut);
}
}
}