CODING HELP Decoding no of ways and printing each decode message

Saumyojit

Senior Member
Joined
Jan 21, 2020
Messages
1,032
Question in short:
print The encoded message in how many ways can it be decoded and print each decoded message of evry encoded message
1 represents A ...26 is z and
04 or 06 ... is invalid
27 , 28 INVALID as there is no Alphabhet above 26

EG:
input : 243
eg:

2 4 3 output BDC

24 3 output will be XC



so 2 ways


Input : 22415
Only one single and other will be in pair ( only for odd length string)

Single is first 2


other pair 24, 15
then,
Output :B X O

Or it can be like this

input: 22415
Single is 2nd position '4'

& others are in Pair

pair is 22,15

Output :
V D O

PLEASE HELP ME WITH THE LOGIC THAT IS PROVIDED IF U THINK its sounds logically CORRECT.
DONT USE DYNAMIC PROGRAMMING






I have to get every possible combinations .
See i thought of in this way
First i will take input of the string ,
then i will divide by i which will start from 2 and end at length of the String . Then, I will divide the string by each value of i .
suppose the string is 8 bit long " 22423146"
'i' tells us in how many parts i am dividing the original input
when i completely divides length , then the String is divided in equal parts

suppose when i=2 then the string is divided to 2242 3146 . This is Invalid.
i=4 (4 parts) , then i get this 22 42 31 46 which is a valid Combination.
when i =8 , then I will get only single : 2 2 4 2 3 1 4 6



  • BUt when i is not a divisor of length '8' (i=3,5,7) then
Observe that space in between parts is always one less than the no of parts
i=5 (5 parts),
it can be 22 4 2 31 46 ( invalid due to 31 and 46)
or if i shift the space in between 2 and 3 to between 3 and 1 .
then i will get a unique arrangement 22 4 23 14 6 (valid)

Now i can rearrange the spaces anyway in between the parts and place them in between two characters to give arise to a new combination, PROVIDED that two spaces don't sit side by side .
This is what i told is the LOGIC but how will i implement in the code in C or JAVA.
The checking part whether it is invalid or valid (Greater than 26 and any character double or single starting with Zero or not ) and matching the characters single or Double to Corresponding Alphabhet ---- that i will do it .
I also know how to extract characters by using charAt(i) ; and I also Know Recursion .
But how to introduce space and how to drift the space from its initial postion to another new position to get another new combination when (length % i !=0) , and how will I divide the string to ' i ' Parts , How to put all of these together . IT's an uphill task for me .




UPDATE:
I have written the code for how to divide string into i equal parts .i has to be a divisor of the length .
where i is divisor of ANY length of input string .

Java:
public class Main{



public static void main(String[] args){

int i=2, j=0;

for(i=2; i< = s.length() ; i=i+2 )

{

String firsthalf=" ";

int index=0;

String s="15641123222211112222";

int len=s.length();

String ar[] = new String ;

int partpoint = len/i;

int control=partpoint;

for(j=0; j<len ; j=j+control)

{

for(i=j; i<partpoint ; i++)

{

char c=s.charAt(i);



firsthalf = firsthalf + c;

}

ar[index]= firsthalf ;

firsthalf= " ";

index= index + 1;

partpoint=partpoint + control;

}



for(i=0; i<ar.length ; i++)

{



System.out.println(ar); / here i am just printing the parts /

System.out.println();

String s3 = ar; .......

then i am not writing the particular fragment of the code

/ picking up the first string from array for a specific value of i ,then I will check whether it is valid or not ; greater than 26 or starting with 0 or not? is in pair or single ....This will happen with all the parts of the input divided to for each value of i .

If the checking is right , then it will be counted as one way .

I have the code in my editor but not writing it here /



}end of for loop



System.out.println();

System.out.println();

ar= " " ; / here my idea is to remove all the elements from the array so that in the next loop when i gets a new value inside ar new set of parts is being inserted.



But how to remove all the existing elements from the array . What is the code./





}

}

}


U just provide the code for the last bit(remove elements from array) and u once check the logic for parts where i is divisor of ANY length of input string . I think this is the one portion of counting the valid ways of DECODING.FOR i = not divisor I am thinking on that



@Dr.Peterson @JeffM @lex
 
Last edited:
...I also Know Recursion .
But how to introduce space and how to drift the space from its initial postion to another new position

The following code is a hint (if you don't immediatly see how this applies to your problem then please think about it for a while)...
Java:
public class Five {
    final static int max=5;
    
    static void ways(int i, String s) {
        if (i == max) {
            System.out.println(s);
        }
        else {
            // Adding 1 must be possible
            ways(i+1, s+" + 1");
            
            if (i+2 <= max) { // Can 2 be added?
                ways(i+2, s+" + 2");
            }
        }
    }
    
    public static void main(String args[]) {
        ways(0, "");
    }
}

This is the output...
Rich (BB code):
javac Five.java && java Five # This line shows how you might compile and run it
 + 1 + 1 + 1 + 1 + 1
 + 1 + 1 + 1 + 2
 + 1 + 1 + 2 + 1
 + 1 + 2 + 1 + 1
 + 1 + 2 + 2
 + 2 + 1 + 1 + 1
 + 2 + 1 + 2
 + 2 + 2 + 1
 
Top