Skip to content

Commit 1564c7e

Browse files
author
manjunatha
committed
Minimize the given string
1 parent d70206c commit 1564c7e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: datastructure/string/EncryptString.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package datastructure.string;
2+
3+
/**
4+
* Minimize the string
5+
* @author Manjunath Asundi
6+
*/
7+
public class EncryptString {
8+
9+
public static String encryptStringUtil(String string) {
10+
StringBuilder encryptedString = new StringBuilder();
11+
int j;
12+
for (int i = 0; i < string.length(); i = j) {
13+
j = i + 1;
14+
while (j < string.length() && string.charAt(i) == string.charAt(j))
15+
j++;
16+
if ((j - i) > 1)
17+
encryptedString.append("" + (j - i) + string.charAt(i));
18+
else
19+
encryptedString.append("" + string.charAt(i));
20+
}
21+
return encryptedString.toString();
22+
}
23+
24+
public static void main(String[] args) {
25+
String string = "aaaaaaaaaabbbcccbbaaabc";
26+
System.out.println("Input String: " + string);
27+
System.out.println("Output String: " + encryptStringUtil(string));
28+
}
29+
}

0 commit comments

Comments
 (0)