atul.it.jss
BAN USER
/*
Write an algorithm to convert the given input string to the expected output string. The input string shall be the length of few million characters. So the output has to be updated in the same string...
Input String = "ABBCDEFGGGGGGGGHHXX..<20 more X>..XYY..."
Expected output = "A1B2C1D1E1F1G8H2X23Y2..."
Note: The count of each character has to be appended with the same character in the output string
*/
public class Problem1 {
/**
* @param args
*/
public String appendFrequency(String s)
{
String result="";
char c[]=s.toCharArray();
int count=0;
int i=0;
char x=c[0];
while(i<c.length)
{
count=0;
x=c[i];
while(i<c.length && c[i]==x)
{
count++;
i++;
}
result=result+x+count;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Problem1().appendFrequency("aabbbbbcccdd"));
}
}
/*
Write an algorithm to convert the given input string to the expected output string. The input string shall be the length of few million characters. So the output has to be updated in the same string...
Input String = "ABBCDEFGGGGGGGGHHXX..<20 more X>..XYY..."
Expected output = "A1B2C1D1E1F1G8H2X23Y2..."
Note: The count of each character has to be appended with the same character in the output string
*/
public class Problem1 {
/**
* @param args
*/
public String appendFrequency(String s)
{
String result="";
char c[]=s.toCharArray();
int count=0;
int i=0;
char x=c[0];
while(i<c.length)
{
count=0;
x=c[i];
while(i<c.length && c[i]==x)
{
count++;
i++;
}
result=result+x+count;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Problem1().appendFrequency("aabbbbbcccdd"));
}
}
- atul.it.jss November 25, 2014