String Compression
Solutions
Approach - Copy characters to new String
Steps
public string Compress(string str)
{
if (str is null)
return str;
string final = "";
int count = 0;
for (int i = 0; i < str.Length; i++)
{
count++;
if (i + 1 >= str.Length || str[i] != str[i + 1])
{
final += str[i + 1 >= str.Length ? str.Length - 1 : i];
if (count > 1)
final += count;
count = 0;
}
}
return final;
}Approach - Copy characters to StringBuilder
Steps
Last updated