Amazon Interview Question
Software Engineer / Developers@sonal....
your code will not work for the strings which contains continuous run length of a character > 9...like aaaaaaaaaa,abbbbbbbbbbbc etc....
you should consider these case as well..
you can use ..itoa(count) and append to string a.
run length encoding :)
0(n) time algorithm :)
void runlengthencoding(char s[],char run[])
{
int n,k=0;
int count;
int i,j;
k=0;
n=strlen(s);
for(i=0;i<=n-1;)
{
run[k]=s[i];
count=1;
for(j=i+1;j<=n-1;j++)
{
if(s[j]!=s[i])
break;
else
count++;
}
i=j;
sprintf(run+k+1,"%d",count);
k=k+1;
while(count!=0)
{
count=count/10;
k++;
}
}
run[k]='\0';
}
public class CompressString
{
public static void main(String[] args)
{
System.out.println("[CompressString] in main()");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string:");
try {
String str= br.readLine();
StringBuffer strBuff=new StringBuffer();
strBuff.append(str.charAt(0));
int len=str.length();
for(int i=1;i<len;)
{
char ch=str.charAt(i);
char chBuff=strBuff.charAt(strBuff.length()-1);
if(ch==chBuff)
{
int num=1;
strBuff.append(num);
while(ch==chBuff )
{
num++;
strBuff.deleteCharAt(strBuff.length()-1);
strBuff.append(num);
i++;
if(i<len)
ch=str.charAt(i);
else
break;
}
}
else
{
strBuff.append(ch);
i++;
}
}
System.out.println("Output String : "+strBuff);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class CompressString
{
public static void main(String[] args)
{
System.out.println("[CompressString] in main()");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string:");
try {
String str= br.readLine();
StringBuffer strBuff=new StringBuffer();
strBuff.append(str.charAt(0));
int len=str.length();
for(int i=1;i<len;)
{
char ch=str.charAt(i);
char chBuff=strBuff.charAt(strBuff.length()-1);
if(ch==chBuff)
{
int num=1;
strBuff.append(num);
while(ch==chBuff )
{
num++;
strBuff.deleteCharAt(strBuff.length()-1);
strBuff.append(num);
i++;
if(i<len)
ch=str.charAt(i);
else
break;
}
}
else
{
strBuff.append(ch);
i++;
}
}
System.out.println("Output String : "+strBuff);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void RunLengh(string s1)
{
char[] arr = s1.ToArray();
string result = string.Empty;
int count = 1;
for (int i = 0; i < s1.Length; i++)
{
if (result.EndsWith(arr[i].ToString()) == true)
count = count + 1;
else if (count > 1)
{
result = result + count.ToString();
count = 1;
result = result + arr[i];
}
else
result = result + arr[i];
}
if (count > 1)
result = result + count.ToString();
Console.WriteLine(result);
}
string encodeRLE(const string &src)
{
size_t len = src.length();
ostringstream os;
for(size_t i = 0; i < len; ) {
size_t j;
int count = 1;
for(j = i + 1; j < len; j++) {
if(src[i] == src[j])
++count;
else
break;
}
if(count != 1)
os << src[i] << count;
else
os << src[i];
i = j;
}
return os.str();
}
private static string consecutiveRepeatCount(char[] arr)
{
string result = "";
int count = 0, max = 0;
int num = arr[0];
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] == arr[i + 1])
{
count++;
if (i + 1 == arr.Length - 1 ) // for last count match
{
result += arr[i];
if (count != 0)
{
int n = count + 1;
result += n;
}
}
}
else
{
result += arr[i];
if (count != 0)
{
int n= count+1;
result += n;
}
if (count > max)
{
num = arr[i];
max = count;
}
if (i + 1 == arr.Length - 1) // for last count print
{
result += arr[i+1];
}
count = 0;
}
}
return result;
}
- Sonal July 21, 2011