A9 Interview Question
Applications DevelopersCountry: India
Interview Type: Phone Interview
public class FindAstrix {
public static void main(String[] args) {
String A = "ABC*EFG", B = "ABCDE*G";
int indexOfA = A.indexOf('*'), indexOfB = B.indexOf('*');
if (A.length() != B.length() || indexOfA == -1 || indexOfB == -1 || indexOfA == indexOfB) {
System.out.println("NOT-POSSIBLE");
} else if (indexOfA > indexOfB) {
System.out.println(_findMissingChar(A, B));
} else if (indexOfA < indexOfB) {
System.out.println(_findMissingChar(B, A));
}
}
private static String _findMissingChar(String str1, String str2) {
String[] parts = str1.split("\\*");
int index = str2.indexOf(parts[0]);
if (index >= 0) {
return parts[0] + str2.charAt(index + 1) + parts[1];
} else {
index = str2.indexOf(parts[1]);
return parts[0] + str2.charAt(index - 1) + parts[1];
}
}
}
import java.io.*;
class MyTest {
public static void main(String[] args)throws IOException {
String s1,s2;
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter two Strings");
s1=reader.readLine();
s2=reader.readLine();
if (!(check(s1) && check(s2)))
System.out.println("not possible");
else
System.out.println(match(s1,s2));
}
public static String match(String s1,String s2){
String s3,s4;
String[] str1=s1.split("\\*");
String[] str2=s2.split("\\*");
int l1=str1.length;
int l2=str2.length;
int l3=l1>=l2?l1:l2;
if (l3==1){
if (str1[0].contains(str2[0])||str2[0].contains(str1[0]))
s3=str1[0].length()>str2[0].length()?str1[0]:str2[0];
else
s3="not possible";
}
else{
if ((str1[0].contains(str2[0])||str2[0].contains(str1[0]))&&(str1[1].contains(str2[1])||str2[1].contains(str1[1]))){
s3=str1[0].length()>str2[0].length()?str1[0]:str2[0];
int l=s3.length();
if (s1.contains(s3))
s4=s2.substring(l,s2.length());
else
s4=s1.substring(l,s1.length());
s3=s3+s4;
}
else
s3="not possible";
}
return s3;
}
public static boolean check(String str){
int count=0;
char c[]=str.toCharArray();
for (int i=0;i<str.length() ;i++ ){
if (c[i]=='*')
count++;
}
if (count>1)
return false;
else return true;
}
}
public static String rearrangeWord(String a,String b)
{
if(a.length() == b.length())
{
char [] strA = a.toCharArray();
char [] strB = b.toCharArray();
for(int x = 0 ; x < a.length() ; x++)
{
if(strA [x] == '*')
{
strA[x] = strB[x];
}
else if(strB[x] == '*')
{
strB[x] = strA[x];
}
}
return String.valueOf(strA);
}
return "Not possible";
}
public static String rearrangeWord(String a,String b)
{
if(a.length() == b.length())
{
char [] strA = a.toCharArray();
char [] strB = b.toCharArray();
for(int x = 0 ; x < a.length() ; x++)
{
if(strA [x] == '*')
{
strA[x] = strB[x];
}
else if(strB[x] == '*')
{
strB[x] = strA[x];
}
}
return String.valueOf(strA);
}
return "Not possible";
}
public static String rearrangeWord(String a,String b)
{
if(a.length() == b.length())
{
char [] strA = a.toCharArray();
char [] strB = b.toCharArray();
for(int x = 0 ; x < a.length() ; x++)
{
if(strA [x] == '*')
{
strA[x] = strB[x];
}
else if(strB[x] == '*')
{
strB[x] = strA[x];
}
}
return String.valueOf(strA);
}
return "Not possible";
}
#include <iostream>
using namespace std;
string findCommon(string a, string b)
{
int i=0, j=0, m=a.size(), n=b.size();
string ans;
while(i<m and j<n)
{
cout << a[i] << " " << b[j] << endl;
if(a[i]==b[j] and a[i]=='*')
{
i++;
j++;
continue;
}
if(a[i]!=b[j] and a[i]!='*' and b[j]!='*')
return "not-possible";
if(a[i]==b[j] and a[i]!='*')
{
ans+=a[i];
i++;
j++;
continue;
}
if(a[i]=='*')
{
if(i==m-1)
{
int temp=b.find('*');
if(temp<j)
ans+=b.substr(j);
else
ans+=b.substr(j, temp-j)+b.substr(temp+1, n-temp-1);
break;
}
while(j<n and a[i+1]!=b[j] and b[j]!='*')
{
ans+=b[j];
j++;
}
if(j==n)
return "not-possible";
else
i++;
continue;
}
if(b[j]=='*')
{
if(j==n-1)
{
int temp=a.find('*');
if(temp<i)
ans+=a.substr(i);
else
ans+=a.substr(i, temp-i)+a.substr(temp+1, m-temp-1);
break;
}
while(i<m and b[j+1]!=a[i] and a[i]!='*')
{
cout << "adding " << a[i] << endl;
ans+=a[i];
i++;
}
if(i==m)
return "not-possible";
else
j++;
continue;
}
}
return ans;
}
int main()
{
cout << findCommon("*c", "d*");
return 0;
}
import java.io.*;
- sandy August 02, 2014import java.util.*;
class Abc
{
public static void main(String args[])throws IOException
{
int a,b;
String s,s1,s2,s3,s4;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//InputStreamReader isr = new InputStreamReader(input);
System.out.println("Enter the 1st string with * in the string at any position");
s=input.readLine();
System.out.println("Enter the 2st string with * in the string at any position");
s1=input.readLine();
a=s.indexOf('*');
b=s1.indexOf('*');
System.out.println("index of string 1 is at "+a);
System.out.println("index of string 2 is at:"+b);
if(a!=b)
System.out.println("not possible");
else{
s2=s.substring(0, +a);
s3=s1.substring(0, +b);
System.out.println(s2 );
System.out.println(s3 );
s4=s2.concat(s3);
System.out.println(s4);
}
}
}
//try it...seems legit to me