Expedia Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
{
package careerCub;
public class StringRemoving {
public static void main(String[] args) {
String s1="A Statement is a Statement";
String s2="Statement a";
String ss=null;
String[] arr1=s2.split("\\s");
String[] arr2=s1.split("\\s");
int flag=-1;
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
flag=arr2[j].compareTo(arr1[i]);
if(flag==0){
ss=s1.replaceFirst(arr2[j], "");
s1=ss;
break;
}
}
}
System.out.println(s1);
}
}
}
public class StringRemoving {
public static void main(String[] args) {
String s1="A Statement is a Statement";
String s2="Statement a";
String ss=null;
String[] arr1=s2.split("\\s");
String[] arr2=s1.split("\\s");
int flag=-1;
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
flag=arr2[j].compareTo(arr1[i]);
if(flag==0){
ss=s1.replaceFirst(arr2[j], "");
s1=ss;
break;
}
}
}
System.out.println(s1);
}
}
// we can keep the list aling with map and keep the
// iterator into this list instead of the int. (index)
// struct entry {
// int index;
// list<entry>::iterator iter;
// // add forn and get the list.begin()
//};
bool comp_int(const pair<int, string> &p1, const pair<int, string> &p2) {
return std::less<int>(p1.first. p2.first);
}
void remove_second(string &s1, string s2) {
if(s1.empty() || s2.empty()) {
return;
}
unordered_multimap<string, int> storage;
typedef unordered_multimap<string, int>::iterator it;
stringstream ss(s1);
int index = 0;
string str;
while(!ss.eof()) {
ss >> str;
//cout << "string is " << str <<"\n";
storage.insert(pair<string, int>(str, index));
index += str.length();
}
ss.str(s2);
index = 0xFFFFFFFF;
while(!ss.eof()) {
ss >> str;
//cout << "string is " << str <<"\n";
pair<it, it> pit = storage.equal_range(str);
if (pit.second == storage.end()) {
continue;
}
it iter = pit.first;
int index_iter = 0;
int counter = 0;
while(iter != pit.second) {
if (index < iter->second) {
index = iter->second;
index_iter = counter;
}
counter++;
}
iter = pit_first + index_iter;
storage.erase(iter);
}
// now iterate iver the hashmap and then sort the entries by the value
vector<pair<int, string> > v;
for(it iter = storage.begin(); it != storage.end(); ++it) {
v.push_back(pair<int, string>(iter->second, iter->first));
}
std::sort(v.begin(), v.end(), comp_int);
// print the vector
}
// This will solution assumes that the str2 contains all the unique tokens
String function(String str1, String str2) {
StringTokenizer stringTokenizer = new StringTokenizer(str2);
while (stringTokenizer.hasMoreTokens()) {
str1 = str1.replaceFirst(stringTokenizer.nextToken()+" ", "");
}
return str1;
}
O(N + M) Time complexity
var removeStrings = function (str1, str2) {
var arrayA = str1.split(' ');
var arrayB = str2.split(' ');
var hashB = {};
var result = [];
for (var i = 0; i < arrayB.length; i++) {
hashB[arrayB[i]] = 1;
}
for (var i = 0; i < arrayA.length; i++) {
if (hashB[arrayA[i]] === undefined) {
result.push(arrayA[i]);
}
else {
hashB[arrayA[i]] = undefined;
}
}
$(result);
}
Create a dictionary out of the second string.
Traverse first string by word and verify if the word is present in the dictionary.
if present then remove it from the dictionary.
else, add the word to the output buffer.
- Orion arm, Laniakea April 30, 2017