Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Decoding no of ways and printing each decode message

Saumyojit

Well-Known Coder
Screenshot (261).png


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

I have tried a bit and wrote pseudocode (not actual java code) so please check it . I have covered three cases till now .


[CODE lang="java" title="first case Input string considering every charcter as a single"]eg:
243 output BDC
24 3 output will be XC
2 34 invalid
so 2 ways


201 --> BA (zero ignore)


although I forgot many things of java



for loop from zero to , less than string length ,i++

char c=s.Char at(i)

in char c each digit storing

if (c=='1' or '2' or...'9')
a[]={A,B,C....J}
int p= character c is being coverted to integer



else {
if c ==0
Then go to the next character of input string
Control will go to for loop and i has to be incremented.
}

p=p-1 ;
As index starts from zero so character 1 means array zero position.


char w= a[p];
corresponding character stored in w

String s1= s1+ space + w;
End of for loop

int count = count +1 ;
Counter one increase for every way of decoding the message
[/CODE]

the above code is only considering every charcter of input string as Single.
[CODE lang="java" title="CASE 2: Grouping case only pairing no single"] (grouping)

Input : 234
23: W
4: D (this is one way)
Output: W D

Input : 234
2: B
34: invalid
This is not Counted as 34 is invalid.

So one way of grouping the input.

Input: 2304
23 (W)
04 (invalid)


String s= taking input from user
If(s.length()%2==0) Only if the length of string is even
{

for loop i=zero , less than string length -1 ,i+2


char c = s.charAt(i);
char j = s. char At (i+1);


String s1=c.concatenate(j);
Two charactersconcatenation
as now we consider grouping case (eg 12,23...)


if (c=='0')
Then Invalid
first digit zero then invalid


else {
if (s1>26)
{
Exit If condition and terminate the for loop as it is invalid
}

else if (if two above cases are not met)
then,


if (s1=='10', '11',..'26')

a1[]={'k' ,...'z'}

int p1 = convert the string
to integer and store in p1

p1=p1 -1 ;



char w1= a1[p];
corresponding character stored in w1

String s2= s2+ space + w1;


End of for loop

int count = count +1 ;

}
End of if



[/CODE]

[CODE lang="java" title="Case 3: Single + pairing (odd length string only)" highlight="60, 63,90"] This is another case

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

Single is first 2


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

Or it can be like this


Single is 3rd position 4 & others are Pair

pair is 22,15

Output :
V D O

Single + pair Code hbe :
Two conditions for this code:
The input has to be of odd length.

i shall be placed on even index (including 0) only of the input String.
i controls the flow of single digit .






String s= taking input from the user

If(s.length()%2!==0)
{


i is for single digit (Even index of INPUT STRING only)

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

char a2={'A', 'B',....,Z};

char single=s.charAt(i);

int p3=convert single variable to integer ;

p3=p3-1;

char w3=a2[p3];


now we have to check whether the single digit index position is less than 2 or not for that:



This is Another event
Where single digit index position is >2

So there is pairing before single and After single

if (i>2){

for(l=0;l<i-1;l++)
{
char x1= s.charAt(l)
char x2= s.charAt(l)

String pair 1=x1 concatenate x2;

int p= convert pair1 to Integer and store in p

p=p-1;

char w5 = a3[p];
Corresponding alphabet w5 representing

s3=s3+" "+w5

} Inside for loop closed


s3=s3 +" "+w3
Single digit is now getting added to s3



After Single digit considering pairing below code


for(j=i+1; j<s.length()-1; j+2)
{

char x2= s.charAt(j)
char y2= s.charAt(j+1)

String pair1= x2.concatenate(y2)


Single digit jodi zero hye
if(w3=='0')
{
Exit if
}

if(x2 =='0')
{
Sopln("invalid")

}
Else if (pair1>26)
{
Exit if condt terminate the inside for loop }

Else if {
If(pair=='10'...'26')
int p7= Convert pair to integer;

p7=p7-1;

char w6=a3[p7]
s3=s3 +" "+ w6
}
For loop closed


int count=+1;
} End of else








if i is less than 2
Extracting the characters after single digit.

Else

{

String s3= s3+w3;
String s3 te single digit corresponding character inserted


after Single digit

for(j=i +1; j<s.length()-1;j+2)
{
char x=s.charAt(j);

char y=s.charAt(j+1);

String pair=x concatenate y;

if(w3 =='0')
{
exit if;
}

if(x=='0')
{
Sopln( "Invalid");
Terminate the inside for loop
}

else if(pair >26)
{
exit if condt terminate the inside for loop
}
else if {
if(pair =='10'....'26')
{
p2=Convert pair to integer

p2=p2-1;
char w2=a3[p2]
s3=s3+" "+ w2

}Inside for loop closed

int count= count+1;
}
If closed


} for loop of i closed

} Top if closed (odd check )[/CODE]


I have done till these . Are the above codes right? logic ?

THere are other cases also which i am thinking
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom