Comments on: Programming Interview Questions 6: Combine Two Strings /2011/10/10/programming-interview-questions-6-combine-two-strings/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-6-combine-two-strings Information Retrieval and Machine Learning Thu, 20 Jun 2013 16:29:00 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Ram /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7599 Ram Tue, 18 Jun 2013 14:31:00 +0000 /?p=551#comment-7599 Brilliant solution ! Thanks !

]]>
By: Sri /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7598 Sri Tue, 18 Jun 2013 14:09:00 +0000 /?p=551#comment-7598 I believe there is an assumption that the strings are unique and different

]]>
By: Assaf /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7596 Assaf Sat, 15 Jun 2013 09:47:00 +0000 /?p=551#comment-7596 Try s1=”aaf”, s2 = “aae”, s3 = “aaaeaf”

a,b & c will each increment twice, at which point

s1[a]=’f', s1[b]=’e', and s3[c] = ‘a’.

I think your handler won’t do anything in that case (both ifs return false), and i don’t see what it could do. Cool idea :) though i suspect backtracking is unavoidable

]]>
By: Assaf /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7595 Assaf Sat, 15 Jun 2013 09:02:00 +0000 /?p=551#comment-7595 Ustav-
Try Your algorithm with input-
s1 = “ab”
s2 = “ae”
s3 = “aeab”.

s3 is a reshuffle of s1 and s2.
but your algorithm will return false, because with the first char (‘a’) you increment s1′s index instead of s2, and in the next iteration you have -
s1[i]=’b’
s2[i]=’a’
s3[i]=’e’

In such a case you’d need to backtrack

]]>
By: Utsav Pandey /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7580 Utsav Pandey Wed, 15 May 2013 00:00:00 +0000 /?p=551#comment-7580 Here is a simple O(n) solution. Recursion is overkill for this problem, unless explicitly asked for.

]]>
By: Melih KARAMAN /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7568 Melih KARAMAN Sun, 31 Mar 2013 01:47:00 +0000 /?p=551#comment-7568 This one includes both:


def isCorrectShuffle(str1,str2,str3):
iter1,iter2,iter3 = (0,0,0)
for iter3 in xrange(len(str3)):
if iter1 < len(str1) and iter2 < len(str2) and
str3[iter3] == str1[iter1] and str3[iter3] == str2[iter2]:
return isCorrectShuffle(str1[iter1+1:],str2[iter2:],str3[iter3+1:])
or
isCorrectShuffle(str1[iter1:],str2[iter2+1:],str3[iter3+1:])
if iter1 < len(str1) and str3[iter3] == str1[iter1]:
iter1 += 1
elif iter2 < len(str2) and str3[iter3] == str2[iter2]:
iter2 += 1
else:
return False;
return (iter1 == len(str1)) and (iter2 == len(str2))

]]>
By: İbrahim Aygül /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7548 İbrahim Aygül Thu, 28 Feb 2013 22:39:00 +0000 /?p=551#comment-7548 No need for recursion and DP. See this post:

]]>
By: İbrahim Aygül /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7547 İbrahim Aygül Thu, 28 Feb 2013 22:32:00 +0000 /?p=551#comment-7547 Order of the letters should be preserved in the third string. XOR cannot detect the order. Although, you can detect if given third string is a permutation of the given two strings with XOR algorithm.

]]>
By: Lifeng Yin /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7544 Lifeng Yin Fri, 08 Feb 2013 18:38:00 +0000 /?p=551#comment-7544 Why don’t you use the XOR algorithm in Find Missing Element? It can be solved in O(n)

]]>
By: Ozan Tabak /2011/10/10/programming-interview-questions-6-combine-two-strings/#comment-7499 Ozan Tabak Sat, 24 Nov 2012 00:52:00 +0000 /?p=551#comment-7499 Here’s my iterative solution written java which handles the case of (str1[i] == str2[i] == shuffledStr[i]) with O(N+M) time complexity and O(N) extra space complexity,

public boolean isCorrectlyShuffled(String str1, String str2,String shuffledStr) {

//handle null strings by converting them to empty strings
str1 = (str1 == null) ? “” : str1;
str2 = (str2 == null) ? “” : str2;
shuffledStr = (shuffledStr == null) ? “” : shuffledStr;

//eliminate some cases by checking string lengths
//and empty-nonempty str1-str2 inputs

if ((str1.length() + str2.length() != shuffledStr.length())
|| ((str1.isEmpty() || str2.isEmpty()) && !(str1 + str2).equals(shuffledStr)))
return false;

//use two index values for two strings
int ind1 = 0;
int ind2 = 0;

//keep a hashmap for the case of (str1[i] == str2[i] == shuffledStr[i])
Map charBackup = new HashMap();

for (int i = 0; i < shuffledStr.length(); i++) {
//count occurences of a char at current indexes of three strings
int occurence = 0;
char currChar = shuffledStr.charAt(i);

if (ind1 < str1.length() && currChar == str1.charAt(ind1)) {
ind1++;
occurence++;
}

if (ind2 0) {
occurence++;
charBackup.put(currChar, –backupLimit);
}

//if a char occured more than once, add the char to the hashmap
//to be used later as a backup
if (occurence == 2) charBackup.put(currChar, backupLimit + 1);

else if (occurence == 0) return false;
}

return true;
}

]]>