Interview Question
Web DevelopersCountry: United States
Interview Type: Phone Interview
public class LetterCount {
public static void main(String[] args) {
String str="aaabbba";
compress(str);
}
public static void compress(String str){
int size = countCompression(str);
if(size >str.length()){
return;
}
StringBuffer myStr = new StringBuffer();
char last = str.charAt(0);
int count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
myStr.append(last);
myStr.append(count);
last = str.charAt(i);
count=1;
}
}
myStr.append(last);
myStr.append(count);
System.out.println(myStr.toString());
}
public static int countCompression(String str){
char last = str.charAt(0);
int size=0,count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
last = str.charAt(i);
size+=1+String.valueOf(count).length();
count=1;
}
}
size+=1+String.valueOf(count).length();
return size;
}
}
/*Compress String with character and its count.
Example: "aaabbba" -> compress -> "a3b3a1”*/
public class Primero_ContarCantidadDeLetrasEnString {
public static void main(String[] args) {
String word = "aaaabccdddef";
System.out.println(" Original Word: " + word + " compressed word " + compressString(word));
String word2 = "abbcccddddeeeeefffff";
System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));
String word3 = "ana";
System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));
}
private static String compressString(String word) {
char[] a = word.toCharArray();
StringBuilder endWord = new StringBuilder();
int count = 1;
System.out.println(a.length);
for (int i = 0; i < a.length; i++) {
if (i != a.length - 1) {
if (a[i] == a[i + 1]) {
count++;
} else {
if (count > 1) {
endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
count = 1;
} else {
endWord.append(String.valueOf(a[i]));
count = 1;
}
}
} else {
if (a[i] == a[i - 1]) {
count++;
endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
} else
endWord.append(String.valueOf(a[i]));
}
}
System.out.println(endWord);
return endWord.toString();
}
}
/*Compress String with character and its count.
Example: "aaabbba" -> compress -> "a3b3a1”*/
public class Primero_ContarCantidadDeLetrasEnString {
public static void main(String[] args) {
String word = "aaaabccdddef";
System.out.println(" Original Word: " + word + " compressed word " + compressString(word));
String word2 = "abbcccddddeeeeefffff";
System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));
String word3 = "ana";
System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));
}
private static String compressString(String word) {
char[] a = word.toCharArray();
StringBuilder endWord = new StringBuilder();
int count = 1;
System.out.println(a.length);
for (int i = 0; i < a.length; i++) {
if (i != a.length - 1) {
if (a[i] == a[i + 1]) {
count++;
} else {
if (count > 1) {
endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
count = 1;
} else {
endWord.append(String.valueOf(a[i]));
count = 1;
}
}
} else {
if (a[i] == a[i - 1]) {
count++;
endWord.append(String.valueOf(count)).append(String.valueOf(a[i]));
} else
endWord.append(String.valueOf(a[i]));
}
}
System.out.println(endWord);
return endWord.toString();
}
}
/*Compress String with character and its count.
Example: "aaabbba" -> compress -> "a3b3a1”*/
public class Primero_ContarCantidadDeLetrasEnString {
public static void main(String[] args) {
String word = "aaaabccdddef";
System.out.println(" Original Word: " + word + " compressed word " + compressString(word));
String word2 = "abbcccddddeeeeefffff";
System.out.println(" Original Word: " + word2 + " compressed word " + compressString(word2));
String word3 = "ana";
System.out.println(" Original Word: " + word3 + " compressed word " + compressString(word3));
}
private static String compressString(String word) {
StringBuilder finalWord = new StringBuilder();
int count = 1;
System.out.println(word.length());
for (int i = 0; i < word.length(); i++) {
if (i != word.length() - 1) {
if (word.charAt(i) == word.charAt(i + 1)) {
count++;
} else {
if (count > 1) {
appendRepetedCharacter(word, finalWord, count, i);
count = 1;
} else {
appendSingleCharacter(word, finalWord, i);
count = 1;
}
}
} else {
if (word.charAt(i) == word.charAt(i-1)) {
count++;
appendRepetedCharacter(word, finalWord, count, i);
} else
appendSingleCharacter(word, finalWord, i);
}
}
System.out.println(finalWord);
return finalWord.toString();
}
private static void appendSingleCharacter(String word, StringBuilder finalWord, int i) {
finalWord.append(String.valueOf(word.charAt(i)));
}
private static void appendRepetedCharacter(String word, StringBuilder finalWord, int count, int i) {
finalWord.append(String.valueOf(count)).append(String.valueOf(word.charAt(i)));
}
}
public static String check(String input) {
Stack<Character> temp = new Stack<>();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (temp.isEmpty())
temp.push(input.charAt(i));
else if (i == (input.length() - 1) && !temp.isEmpty()) {
if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
output.append(input.charAt(i)).append(1);
} else
output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
} else if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
temp.removeAllElements();
temp.push(input.charAt(i));
} else
temp.push(input.charAt(i));
}
return output.toString();
}
public static String check(String input) {
Stack<Character> temp = new Stack<>();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (temp.isEmpty())
temp.push(input.charAt(i));
else if (i == (input.length() - 1) && !temp.isEmpty()) {
if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
output.append(input.charAt(i)).append(1);
} else
output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
} else if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
temp.removeAllElements();
temp.push(input.charAt(i));
} else
temp.push(input.charAt(i));
}
return output.toString();
}
public static String check(String input) {
Stack<Character> temp = new Stack<>();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (temp.isEmpty())
temp.push(input.charAt(i));
else if (i == (input.length() - 1) && !temp.isEmpty()) {
if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
output.append(input.charAt(i)).append(1);
} else
output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
} else if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
temp.removeAllElements();
temp.push(input.charAt(i));
} else
temp.push(input.charAt(i));
}
return output.toString();
}
public static String check(String input) {
Stack<Character> temp = new Stack<>();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (temp.isEmpty())
temp.push(input.charAt(i));
else if (i == (input.length() - 1) && !temp.isEmpty()) {
if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
output.append(input.charAt(i)).append(1);
} else
output.append(temp.peek()).append(Integer.toString(temp.size() + 1));
} else if (temp.peek() != input.charAt(i)) {
output.append(temp.peek()).append(Integer.toString(temp.size()));
temp.removeAllElements();
temp.push(input.charAt(i));
} else
temp.push(input.charAt(i));
}
return output.toString();
}
Note: assuming we have to count not just consecutive chars. we are counting all chars irrespective of whether they are consecutive.
- gkr July 31, 2017public static string Compress(string str)
{
string compressed = "";
Dictionary<char, int> sDict = new Dictionary<char, int>();
foreach (char c in str)
{
if (!sDict.ContainsKey(c))
{
sDict.Add(c, 1);
}
else
{
sDict[c]++;
}
}
foreach (KeyValuePair<char, int> keyvalue in sDict)
{
compressed = compressed + keyvalue.Key + keyvalue.Value;
}
return compressed;
}