You are on page 1of 13

Problem Statement 1:

Problem Statement:

Consider one string as input. You have to check that given strings with single backward and forward shift
are same or not. If they are same then print 1 otherwise print 0.

Hint: -

Backward Shift: A single circular rotation of the string in which the first character becomes the last
character and all other characters are shifted one index to the left. For example, abcde becomes bcdea
after one left shift.

Forward Shift: A single circular rotation of the string in which the last character becomes the first
character and all other characters are shifted to the right. For example, abcde becomes eabcd after one
right shift

Instructions:

 System doesn’t allow any kind of hard coded input value/ values.
 Written program code by candidate will be verified against the inputs which are supplied from
the system.
 For more clarification, please read below points carefully till the end.

Constraints:
String str should not allowed space, special character and numbers.
String str should only in English language.

Examples:

Examples -1:

Input:

sfdlmnop

Output:

Examples - 2:

Input:

mama

Output:

Explanation:
In first example, string is “sfdlmnop”.
Forward Shift:- fdlmnops
Backward Shift:- psfdlmnop
Above both strings are not equal. So output is 0.

In second example, string is “mama”.


Forward Shift:- amam
Backward Shift:- amam
Above both strings are equal. So output is 1.
Input Format:

 Candidate has to write the code to accept single string value for str without any additional
message.

Output Format:

 Written program code should generate output as single integer value i.e. 0 or 1 which represent
both forward and backward shift strings are equal or not.
 Additional message in output will cause to failure of test cases.

CLanguage

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

void checkLeftRightString(char str[])

int len=0;

len=strlen(str);

bool check = true;

for (int i = 0; i <len; i++) {

if (str[i] != str[(i + 2) % len]) {

check = false;

break;

}
}

if (check)

printf("1");

else

printf("0");

int main()

char str[100];

scanf("%s",str);

checkLeftRightString(str);

return 0;

C++Language

#include <iostream>
using namespace std;

void checkLeftRightString(string str)

int len = str.length();

bool check = true;

for (int i = 0; i <len; i++) {

if (str[i] != str[(i + 2) % len]) {

check = false;

break;

if (check)
cout<< "1" <<endl;

else

cout<< "0" <<endl;

int main()

string str;

cin>>str;

checkLeftRightString(str);

return 0;

Java

import java.util.*;

public class MyClass

public static void checkLeftRightString (String str)

int len = str.length ();

boolean check = true;

for (int i = 0; i <len; i++)

if (str.charAt (i) != str.charAt ((i + 2) % len))

check = false;

break;

}
}

if (check)

System.out.println ("1");

else

System.out.println ("0");

public static void main (String[]args)

Scanner sc = new Scanner (System.in);

String str = sc.next ();

checkLeftRightString(str);

Python

def checkLeftRightString (string):

length = len(string)

check = True

for i in range(length):

if string[i] != string[(i + 2) % length]:

check = False

break

if check :

print("1")
else:

print("0")

string = input()

checkLeftRightString(string)

C#
using System.IO;
using System;

class MyClass
{
public static void checkLeftRightString (String str)
{
int len = str.Length;
bool check = true;
for (int i = 0; i <len; i++)
{
if (str[i] != str[(i + 2) % len])
{
check = false;
break;
}
}
if (check)
Console.WriteLine("1");
else
Console.WriteLine("0");
}
static void Main(String[] args)
{
String str = Console.ReadLine();
checkLeftRightString(str);

}
}
Problem Statement 2:

Way of correct wrong word to correct word by removing zero or more characters.

Given strings i.e. S1 (for wrong word), S2( for correct word), find out number of was where we can
transform wrong word to correct word by removing characters from wrong word .

Example 1 :

Input :
Indiiian  String S1 i.e. wrong word
Indian  String S2 i.e. correct word
Output :

Explanation: Three ways will be -> "ind..ian", "indi..an" , “ind.i.an”

"." is where character is removed.

Example 2 :

Input :
ggoog  String S1 i.e. wrong word
go  String S2 i.e. correct word
Output :

Explanation: four ways will be -> "g.o..", "g..o.", “.go..” and ”.g.o.”

"." is where characters are removed.

Input Format:

 Two string (separated by new line) taken from user.

Output Format:

 Single integer value.


C Code
#include<stdio.h>

#include <string.h>

int wayToChange(char s1[], char s2[],int count1, int count2)

int n = count1, m = count2;

if (m == 0)

return 1;

int arr[m][n];

memset(arr, 0, sizeof(arr));

for (int i = 0; i< m; i++) {

for (int j = i; j < n; j++) {

if (i == 0) {

if (j == 0)

arr[i][j] = (s1[j] == s2[i]) ?1 : 0;

else if (s1[j] == s2[i])

arr[i][j] = arr[i][j - 1] + 1;

else

arr[i][j] = arr[i][j - 1];

else {

if (s1[j] == s2[i])

arr[i][j] = arr[i][j - 1] + arr[i - 1][j - 1];

else

arr[i][j] = arr[i][j - 1];

}
}

return arr[m - 1][n - 1];

int main()

int count1,count2;

char s1[100];

char s2[100];

//scanf("%s%s",&s1,&s2);

fgets(s1, sizeof(s1), stdin);

fgets(s2, sizeof(s2), stdin);

count1=strlen(s1);

count2=strlen(s2);

printf("%d",wayToChange(s1, s2,count1,count2));

return 0;

C++ Code

#include <bits/stdc++.h>
using namespace std;

int wayToChange(string s1, string s2)


{
int n = s1.size(), m = s2.size();
// cout<<n<<" "<<m<<"\n";
if (m == 0)
return 1;

int arr[m][n];
memset(arr, 0, sizeof(arr));
for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
if (i == 0) {
if (j == 0)
arr[i][j] = (s1[j] == s2[i]) ? 1 : 0;
else if (s1[j] == s2[i])
arr[i][j] = arr[i][j - 1] + 1;
else
arr[i][j] = arr[i][j - 1];
}
else {
if (s1[j] == s2[i])
arr[i][j] = arr[i][j - 1] + arr[i - 1][j - 1];
else
arr[i][j] = arr[i][j - 1];
}
}
}

return arr[m - 1][n - 1];


}
int main()
{
string s1,s2;
cin>>s1>>s2;
cout << wayToChange(s1, s2) << endl;
return 0;
}

Java Code
import java.util.Scanner;

public class MyClass {

static int wayToChange(String s1, String s2)


{
int n = s1.length(), m = s2.length();
if (m == 0) {
return 1;
}

int arr[][] = new int[m][n];


for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
if (i == 0) {
if (j == 0) {
arr[i][j] = (s1.charAt(j) == s2.charAt(i)) ? 1 : 0;
}
else if (s1.charAt(j) == s2.charAt(i)) {
arr[i][j] = arr[i][j - 1] + 1;
}
else {
arr[i][j] = arr[i][j - 1];
}
}
else if (s1.charAt(j) == s2.charAt(i)) {
arr[i][j] = arr[i][j - 1]
+ arr[i - 1][j - 1];
}
else {
arr[i][j] = arr[i][j - 1];
}
}
}
return arr[m - 1][n - 1];
}

public static void main(String[] args)


{
String s1,s2;
Scanner sc = new Scanner(System.in);
s1 = sc.next();
s2=sc.next();
System.out.println(wayToChange(s1, s2));
}
}

Python Code
def wayToChange(s1, s2):
n = len(s1)
m = len(s2)
if m == 0:
return 1

arr = [[0] * (n) for _ in range(m)]


for i in range(m):
for j in range(i, n):
if i == 0:
if j == 0:
if s1[j] == s2[i]:
arr[i][j] = 1
else:
arr[i][j] = 0
elif s1[j] ==s2[i]:
arr[i][j] = arr[i][j - 1] + 1
else:
arr[i][j] = arr[i][j - 1]
else:
if s1[j] == s2[i]:
arr[i][j] = (arr[i][j - 1] +
arr[i - 1][j - 1])
else:
arr[i][j] = arr[i][j - 1]
return arr[m - 1][n - 1]

if __name__ == "__main__":
string1 = input();
string2 = input();
print(wayToChange(string1, string2))

C# Code
using System.IO;
using System;

class MyClass {

static int wayToChange(String s1, String s2)


{
int n = s1.Length, m = s2.Length;
if (m == 0) {
return 1;
}

int[,] arr = new int[m,n];


for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
if (i == 0) {
if (j == 0) {
arr[i,j] = (s1[j] == s2[i]) ? 1 : 0;
}
else if (s1[j] == s2[i]) {
arr[i,j] = arr[i,j - 1] + 1;
}
else {
arr[i,j] = arr[i,j - 1];
}
}
else if (s1[j] == s2[i]) {
arr[i,j] = arr[i,j - 1]
+ arr[i - 1,j - 1];
}
else {
arr[i,j] = arr[i,j - 1];
}
}
}
return arr[m - 1,n - 1];
}

public static void Main(String[] args)


{
String s1,s2;
s1 =Console.ReadLine();
s2=Console.ReadLine();
Console.WriteLine(wayToChange(s1, s2));
}
}

You might also like