简体   繁体   中英

Reverse encoded string

i am asking for help on how i would go about reversing my code so that the input 'A2B5C2' will give me the output 'AABBBBBCC', any suggestions?

Thanks

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
        System.out.print(str.charAt(i));
        System.out.print(count);
    }
}

public static void main(String[] args) {
    String str = "AABBBBBCC";
    printRLE(str);
}

To get the case, the number will more than 9, I'd suggest a simple regex to match letter+number , then just repeat the letter the number of times you need :

static String getRevRLE(String str) {
    StringBuilder res = new StringBuilder();
    Matcher m = Pattern.compile("([a-zA-Z][0-9]+)").matcher(str);
    while (m.find()) {
        String g = m.group();
        res.append(g.substring(0, 1).repeat(Integer.parseInt(g.substring(1))));
    }
    return res.toString();
}

Using the Streams API you can reduce to

static String getRevRLE(String str) {
    return Pattern.compile("([a-zA-Z][0-9]+)").matcher(str).results()
                  .map(MatchResult::group)
                  .map(g -> g.substring(0, 1).repeat(Integer.parseInt(g.substring(1))))
                  .collect(Collectors.joining());
}

Testing

public static void main(String[] args) {
    String str = "AABBBBBCCCCCCCCCCCCCCCCCCCC";
    String rle = getRLE(str);
    String res = getRevRLE(rle);
    System.out.println(res + " " + res.equals(str)); // AABBBBBCCCCCCCCCCCCCCCCCCCC true
}

Here you go:

public static String encode(String input) {
        String output = "";

        while (true) {
            char c = input.charAt(0);

            String countStr = "";
            char current;
            for (int i = 1; i < input.length() && Character.isDigit(current = input.charAt(i)); i++)
                countStr += current;

            int count = Integer.parseInt(countStr);
            for (int i = 0; i < count; i++)
                output += c;

            int trimLength = 1 + countStr.length();
            if (trimLength >= input.length())
                return output;
            else
                input = input.substring(trimLength);
        }
    }

You can do this task like this:

  public static String printRLE(String str) {
    int n = str.length();
    String result = "";

    for (int i = 0; i < n - 1; i++) {

      char ch = str.charAt(i);
      if (!Character.isDigit(ch)) {

        if (Character.isDigit(str.charAt(i + 1))) {
          int fi = i + 1;
          i += 2;
          while (i < n && Character.isDigit(str.charAt(i))) i++;

          int repeat = Integer.parseInt(str.substring(fi, i));
          result += String.valueOf(ch).repeat(repeat);
          i--;
        } else result += ch;
      }
    }
    return result;
  }

  public static void main(String[] args) {
    String str = "10A10B32C1";
    System.out.println(printRLE(str));
  }

, output

AAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM