- public class RLE_encoding {
- public static void printRLE(String str)
- {
- int n = str.length();
- for (int i = 0; i < n; i++) {
- // Count occurrences of current character
- int count = 1;
- while (i < n - 1 &&
- str.charAt(i) == str.charAt(i + 1)) {
- count++;
- i++;
- }
- // Print character and its count
- if(count<3) {
- System.out.print(0);
- System.out.print(str.charAt(i));
- }else {
- System.out.print(count);
- System.out.print(str.charAt(i));
- }
- }
- System.out.println();
- }
- public static void main(String[] args)
- {
- String Ch1 = "ABABABABABABABABABABABABABABABABABAB";
- printRLE(Ch1);
- String Ch2 = "AAAAAABBBBBBAAAAAABBBBBBAAAAAABBBBBB";
- printRLE(Ch2);
- String Ch3 = "AAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBB";
- printRLE(Ch3);
- }
- }
[text] RLE
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- RLE
- Paste Code
- 05 May-2021
- 1.11 Kb
You can Share it: