Jump to content

Determine if a string is collapsible: Difference between revisions

Line 684:
+-----------------------------------------------------------------------------------+---------------------------------------------------------------------------------+
 
</pre>
 
=={{header|Java}}==
<lang java>
// Title: Determine if a string is collapsible
 
public class StringCollapsible {
 
public static void main(String[] args) {
for ( String s : new String[] {
"",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111117777888",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
" --- Harry S Truman ",
"122333444455555666666777777788888888999999999",
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
"headmistressship"}) {
String result = collapse(s);
System.out.printf("old: %2d <<<%s>>>%nnew: %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
}
}
private static String collapse(String in) {
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < in.length() ; i++ ) {
if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
sb.append(in.charAt(i));
}
}
return sb.toString();
}
 
}
</lang>
 
{{out}}
<pre>
old: 0 <<<>>>
new: 0 <<<>>>
 
old: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
new: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
old: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
new: 4 <<<.178>>>
 
old: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
new: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
old: 72 <<< --- Harry S Truman >>>
new: 17 <<< - Hary S Truman >>>
 
old: 45 <<<122333444455555666666777777788888888999999999>>>
new: 9 <<<123456789>>>
 
old: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>>
new: 77 <<<The beter the 4-whel drive, the further you'l be from help when ya get stuck!>>>
 
old: 16 <<<headmistressship>>>
new: 14 <<<headmistreship>>>
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.