Question in short:
print 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
eg:
243 output BDC
24 3 output will be XC
2 34 invalid
so 2 ways
The input is a String
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
I have tried a bit and wrote pseudocode (not actual java code) so please check it . I have covered three cases till now .
first case Input string considering every charcter as a single:
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
the above code is only considering every charcter of input string as Single.
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
Case 3: Single + pairing (odd length string only):
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
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 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];
Single digit corresponding alphabet in w3
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+2)
{
char x1= s.charAt(l)
char x2= s.charAt(l+1)
String pair 1=x1 concatenate x2;
int p= convert pair1 to Integer and store in p
p=p-1;
char w5 = a2[p];
Corresponding alphabet w5 represent korbe
s3=s3+" "+w5
} Inside for loop closed
s3=s3 +" "+w3
Single digit adding to s3
after Single digit pairing
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=a2[p7]
s3=s3 +" "+ w6
}
For loop closed
int count=+1;
} End of else
i jodi 2 r less hye
Extracting the characters after single digit.
Else
{
String s3= s3+w3;
in String s3 single digit r corresponding character inserted
after Single digit pairing storing them in pair Variable
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=a2[p2]
s3=s3+" "+ w2
}Inside for loop closed
int count= count+1;
}
If closed
} i for loop closed
} Top if closed (odd check)
I have done till these . Are the above codes right logically ?
THere are other cases still left which i am thinking