I want to return each character just after its consecutive occurrences
for example
Input : GeeeEEKKKss
Output : 1G3e2E3K2s
Input : ccccOddEEE
Output : 4c1O2d3E
I have developed this piece of code and it works fine but I am looking if I can fix this problem with java 8 with lambda expression or if there is a better solution for this.
public static String encode(String plaintext ) { String str = ""; for (int i = 0; i < plaintext.length(); i++) { // Counting occurrences of s[i] int count = 1; while (i + 1 < plaintext.length() && plaintext.charAt(i) == plaintext.charAt(i + 1)) { i++; count++; } str += String.valueOf(count) + plaintext.charAt(i); } return str; }
https://stackoverflow.com/questions/66501133/return-each-character-just-after-its-consecutive-occurrences March 06, 2021 at 08:12AM
没有评论:
发表评论