Cracking the Coding Interview Question 1.6, what does the java code return here?

I am studying from Cracking the Coding Interview, and I was doing problem 1.6 which bascially says: "String Compression: Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z)."

I am using StringBuilder, but I need help understanding the code, can anyone please explain what does it return here? Like what does this mean exactly "compressed.length() < str.length() ? compressed.toString() : str;"? I dont understand the <,?,: This java also

1 String compress(String str) {
2 StringBuilder compressed= new StringBuilder();
3 int countConsecutive = 0;
4 for (int i= 0; i < str.length(); i++) {
5 countConsecutive++;
7 /* If next character is different than current, append this char to result.*/
8 if (i + 1 >= str.length() I I str.charAt(i) != str.charAt(i + 1)) {
9 compressed.append(str.charAt(i));
10 compressed.append(countConsecutive);
11 countConsecutive = 0;
12 }
13 }
14 return compressed.length() < str.length() ? compressed.toString() : str;

Read more here:

Content Attribution

This content was originally published by Drdeath7000 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: