Decode the string
An encoded string (s) is given, the task is to decode it. The pattern in which the strings were encoded
were as follows
original string: abbbababbbababbbab
encoded string : "3[a3[b]1[ab]]".
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow.
Each test case contains a string s.
Output:
For each test case in a new line print the required decoded string.
Constraints:
1<=T<=10
1<=length of the string <=30
Example:
Input:
1[b]
3[b2[ca]]
Output:
bcacabcacabcaca
Examples:
Input : str[] = "1[b]"
Output : b
Input : str[] = "2[ab]"
Output : abab
Input : str[] = "2[a2[b]]"
Output : abbabb
Input : str[] = "3[b2[ca]]"
Output : bcacabcacabcaca
Solution
import java.util.Stack;
class Test
{
// Returns decoded string for 'str'
static String decode(String str)
{
Stack<Integer> integerstack = new Stack<>();
Stack<Character> stringstack = new Stack<>();
String temp = "", result = "";
// Traversing the string
for (int i = 0; i < str.length(); i++)
{
int count = 0;
// If number, convert it into number
// and push it into integerstack.
if (Character.isDigit(str.charAt(i)))
{
while (Character.isDigit(str.charAt(i)))
{
count = count * 10 + str.charAt(i) - '0';
i++;
}
i--;
integerstack.push(count);
}
// If closing bracket ']', pop elemment until
// '[' opening bracket is not found in the
// character stack.
else if (str.charAt(i) == ']')
{
temp = "";
count = 0;
if (!integerstack.isEmpty())
{
count = integerstack.peek();
integerstack.pop();
}
while (!stringstack.isEmpty() && stringstack.peek()!='[' )
{
temp = stringstack.peek() + temp;
stringstack.pop();
}
if (!stringstack.empty() && stringstack.peek() == '[')
stringstack.pop();
// Repeating the popped string 'temo' count
// number of times.
for (int j = 0; j < count; j++)
result = result + temp;
// Push it in the character stack.
for (int j = 0; j < result.length(); j++)
stringstack.push(result.charAt(j));
result = "";
}
// If '[' opening bracket, push it into character stack.
else if (str.charAt(i) == '[')
{
if (Character.isDigit(str.charAt(i-1)))
stringstack.push(str.charAt(i));
else
{
stringstack.push(str.charAt(i));
integerstack.push(1);
}
}
else
stringstack.push(str.charAt(i));
}
// Pop all the elmenet, make a string and return.
while (!stringstack.isEmpty())
{
result = stringstack.peek() + result;
stringstack.pop();
}
return result;
}
// Driver method
public static void main(String args[])
{
String str = "3[b2[ca]]";
System.out.println(decode(str));
}
}
Middle Pattern
Given an odd length word your task is to complete the function printPattern that takes a string s as
its argument and prints it from the middle of the word such that it follows the following pattern.
Input: PROGRAM Input: RAT
Output: Output:
G A
GR AT
GRA ATR
GRAM
GRAMP
GRAMPR
GRAMPRO
The above is proper shaped pattern for the test case, but when
printed in a single line it becomes as shown in the output.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases
follow. Each test case consists of a single line containing an odd length string s.
Output:
Corresponding to each test case in a new line print the pattern in a single line where each row of
the pattern is separated by a "$" instead of a new line.
Constraints:
1 ≤ T ≤ 20
1<=size of string(s)<=20
Example(To be used only for expected ouput):
Input
2
PROGRAM
RAT
Output
G$
GR$
GRA$
GRAM$
GRAMP$
GRAMPR$
GRAMPRO$
A$
AT$
ATR$
Solution
void printPattern(string s)
{
int n=s.length();
int sp=2*n-2;
string x=s.substr(0,n/2);
string y=s.substr(n/2,n);
y+=x;
for(int i=0;i<n;i++)
{
string r = y.substr(0,i+1);
for(int j=0;j<sp;j++)
cout<<" ";
cout<<r<<"$";
sp-=2;
}
}
Pattern:
package com.javainterviewpoint;
import java.util.Scanner;
public class Pattern5
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows to print the pattern
");
int rows = scanner.nextInt();
System.out.println("** Printing the pattern... **");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
Enter the number of rows to print the pattern
5
** Printing the pattern... **
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Program to print the berth of given railway
seat number
Given a railway seat number, the task is to check whether it is a valid seat number or not.
Also print its berth type i.e lower berth, middle berth, upper berth, side lower berth, side
upper berth as per the figure below.
Examples:
Input: 10
Output: middle berth
Input: 7
Output: side lower berth
import java .io.*;
class ttb
{
// Function for
// printing berth type
static void berth_type(int s)
{
if (s > 0 && s < 73)
if (s % 8 == 1 ||
s % 8 == 4)
System.out.println(s +
" is lower berth");
else if (s % 8 == 2 ||
s % 8 == 5)
System.out.println(s +
" is middle berth");
else if(s % 8 == 3 ||
s % 8 == 6)
System.out.println(s +
" is upper berth");
else if(s % 8 == 7)
System.out.println(s +
" is side lower berth");
else
System.out.println(s +
" is side upper berth");
else
System.out.println(s +
" invalid seat number");
}
// Driver code
public static void main(String[] args)
{
int s = 10;
berth_type(s); // fxn call for berth type
s = 7;
berth_type(s); // fxn call for berth type
s = 0;
berth_type(s); // fxn call for berth type
}
}
Challenge
Have the function LetterChanges(str) take the str parameter being passed and modify it using the
following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c
becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally
return this modified string.
Sample Test Cases
Input:"hello*3"
Output:"Ifmmp*3"
Input:"fun times!"
Output:"gvO Ujnft!"
Solution
import java.util.*;
import java.io.*;
class Main {
public static String LetterChanges(String str) {
StringBuilder result = new StringBuilder();
char[] strChars = str.toLowerCase().toCharArray();
char[] replacements = {'b', 'c', 'd', 'E', 'f', 'g', 'h', 'I', 'j', 'k', 'l', 'm', 'n', 'O', 'p', 'q', 'r', 's', 't', 'U', 'v',
'w', 'x', 'y', 'z', 'A'};
for (int i = 0; i < str.length(); i++) {
result.append(strChars[i] >= 'a' && strChars[i] <= 'z' ? replacements[strChars[i] - 97] :
strChars[i]);
}
return result.toString();
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(LetterChanges(s.nextLine()));
}