Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added StringAlgorithms/RunLength.class
Binary file not shown.
34 changes: 34 additions & 0 deletions StringAlgorithms/RunLength.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package StringAlgorithms;

import java.util.Scanner;

/*
* Runlength - given aabbbaa converts to a2b3a2.
*/
public class RunLength {

public static void main(String[] args) {

@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter input string");
String input = scanner.nextLine();
System.out.println("Input String is: " + input);

int N = input.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
int runLen = 1;
while (i + 1 < N && input.charAt(i) == input.charAt(i + 1)) {
runLen++;
i++;
}

sb.append(input.charAt(i));
sb.append(runLen);
}

System.out.println(sb.toString());
}

}