Home > Blockchain >  Implement an encoder in java
Implement an encoder in java

Time:09-22

i have an assessment in java that i'm having hard times to solve. This is it:

implement the encode(plainText) method which returns an encoded message. it receives a plainTest parameter, which is a character string for example :

aaaabcccaaa

you must encode it by counting the consecutive occurrences of each letter, for example in aaaabcccaaa, there is:

  • 4 times the letter a
  • then 1 b
  • then 3 c
  • then 3 a

Therefore you must return the string 4a1b3c3a

Constraints:

  • plainText is a lowercase character string (between a and z)
  • plaintext is never null and has a maximum size of 15,000 characters

Example:

  • PlainText

    aabaa

  • CypherText

    2a1b2a


class Solution {
     public static String encode(String plainText){
       //write your code here
     return "";
}

So i did that code below but i think it is wrong:

class Solution {

    public static String encode(String plainText){
              String str = null;
             for(char i : plainText.toCharArray()){
                  if(plainText.charAt(i)=='a'){
                return getCharCount(plainText,"a") "a";
                     
                     }
          }
        return str;

    }

    public static long getCharCount(String str,String val){
        long count= str.chars().mapToObj(e->String.valueOf((char)e)).filter(e->e.equals(val))
                .count();
        System.out.println("occurence of a :"   count);
        return count;
    }



    public static void main(String[] args) {
        String plainText="aaaabccaaa";
        System.out.println(Solution_Encode.encode(plainText));
        getCharCount(plainText,"a");

    }
}


I have only succeeded to display the occurence of the letter a which is 7 and which is not what the assignment asked for. i dont know how to manage in order to have as a result:

  • PlainText(input)

    aabaa

  • CypherText(output)

    2a1b2a

Can you give me a hand please?

CodePudding user response:

You can encode it like below:

Start with setting the first character in string as as current context. Initial count as 1 to it. Then iterate one by one to each character in string. Check if that is same as previous value or not (at initial it will be compared with first character). If same increase the count. If not same then ass the character in context to encodedString with count and change the context to next character and set the count back to 1. As in below code:

public static String encode(String plainText) {
    char currentChar = plainText.charAt(0);
    int count = 1;

    StringBuilder encodedString = new StringBuilder();

    for (int i = 1; i < plainText.length(); i  ) {
      char nextChar = plainText.charAt(i);
      if (currentChar == nextChar) {
        count  ;
      } else {
        encodedString.append(count).append(currentChar);
        currentChar = nextChar;
        count = 1;
      }
    }

    encodedString.append(count).append(currentChar);
    return encodedString.toString();
}
  • Related