Problem 1 String Encoding
Input: GGGGrrrt
O/p = 3G3r1t
all alphanumeric chars are to be considered
...
public static String collapseString(String inputString) {
if (inputString.length()<=0)
return "";
String ans ="";
for (int i =0;i<inputString.length();i++)
{
int count =1;
char c = inputString.charAt(i);
for (int j =i+1;j<inputString.length();j++)
{
if (inputString.charAt(j)!=inputString.charAt(i)){
break;}
else
count++;
i++;
}
ans+= count+""+c;
}
return ans;
}
....
Problem 2 Encoding Decoding String
Encode or Decocde string with given key , operation 1 is encoding 2 is decoding
Example
1
Open
123
output
Oppeeen
Example 2
2
Oppeeen
123
output
Open
...
static String ans ="";
public static boolean Encoding(String msg, String Key)
{
//String ans = "";
int i=0,j=0;
while (i<msg.length() && j<Key.length())
{
int n=0;
try {
n = Integer.parseInt(Key.charAt(j) + "");
}
catch (Exception e)
{
return false;
}
char c = msg.charAt(i);
if (n<0)
return false;
for(int m =0;m<n;m++)
ans = ans+ ""+ c;
i++;j++;
}
if (i<msg.length())
ans += msg.substring(i,msg.length());
return true;
}
public static boolean Decoding(String msg, String Key)
{
int i=0,j=0;
while (i<msg.length() && j<Key.length())
{
int n=0;
try {
n = Integer.parseInt(Key.charAt(j) + "");
}
catch (Exception e)
{
return false;
}
char c = msg.charAt(i);
int count=1;
int m = i +1;
for (;m<msg.length();m++)
{
if (c!=msg.charAt(m))
break;
count++;
i++;
}
if (count!=n)
return false;
else
ans += c;
i++;j++;
}
if (i<msg.length())
ans += msg.substring(i,msg.length());
return true;
}
public static String secureChannel(int operation, String message, String key) {
ans ="";
if(message.length()<=0)
return "-1";
if (operation==1 && Encoding(message,key))
return ans;
else if (operation==2 && Decoding(message,key))
return ans;
return "-1";
}...