unknown Interview Question
SDE-2sCountry: India
Interview Type: Phone Interview
there is a problem is solution in line
"int present[256] = {0, };"
The problem is in hard-coded value 256. Because symbols with code greater than 255 exist.
With s2 = "1234" and s1 = "ĐÁ" the program fails: it gives the output "ĐÁ", while it should be null.
To improve this, we can create one more loop to obtain the greatest char code as follows (c#):
string bothStrings = s1 + s2;
int greatestCode = 0;
for (int i = 0; i < bothStrings.Length; i++) {
if ( greatestCode < bothStrings[i] ) {
greatestCode = bothStrings[i];
}
}
bool[] present = new bool[ greatestCode + 1 ];
Or, even better to apply sort
string bothStrings = s1 + s2;
var chArr = bothStrings.ToCharArray();
Array.Sort( chArr );
int greatestCode = chArr[ chArr.Length - 1 ];
So, the complexity will be O(3n+2k) against O(2n+k) in initial solution. Almost the same, but without the bug.
I believe that condition should be the "smallest" substring. Otherwise the largest substring would be s1 itself (if it contains all characters from s2).
// C# implementation
public class StringSearcher
{
public static string GetMax(string s1, string s2)
{
if (string.IsNullOrEmpty(s2))
return string.Empty;
var charSet = new HashSet<char>(s2);
StringBuilder sb = null;
string current = string.Empty;
foreach (char c in s1)
if (false == charSet.Contains(c))
{
if (sb != null)
{
if (sb.Length > current.Length)
current = sb.ToString();
sb = null;
}
}
else
{
if (sb == null)
sb = new StringBuilder(s1.Length); // give length of s1 to avoid reallocations
sb.Append(c);
}
// the case when there's a last stringbuilder and after it no character was skipped.
if (sb != null && sb.Length > current.Length)
current = sb.ToString();
return current;
}
}
c# implementation.
O(k*n), where k, n - lengths of 2 given strings.
static private string GetLargestSubstring( string s1, string s2 ) {
var start = -1;
var end = -1;
var res = string.Empty;
var tmpRes = string.Empty;
for( int i = 0; i < s1.Length; i++ ) {
for( int j = 0; j < s2.Length; j++ ) {
if( s1[ i ] != s2[ j ] ){
end = i;
} else {
if( start == -1 ) start = i;
end = -1;
tmpRes += s1[ i ];
break;
}
}
if( end != -1 && start != -1) {
start = -1;
if( tmpRes.Length > res.Length ){
res = tmpRes;
}
tmpRes = string.Empty;
}
}
return res;
}
O(k + n)
Java code:
import java.util.HashMap;
public class MaxSubstring {
public static String maxSubstring(String s1, String s2) {
HashMap<Character, Boolean> s2CharsMap = new HashMap<>();
// mapping all unique chars
char[] s2Chars = s2.toCharArray();
for (int i = 0; i < s2Chars.length; i++) {
s2CharsMap.put(s2Chars[i], true);
}
// now ready to iterate over s1 and get substrings
char[] s1Chars = s1.toCharArray();
String maxSubstring = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s1Chars.length; i++) {
if (s2CharsMap.get(s1Chars[i]) != null) {
sb.append(s1Chars[i]);
} else if (sb.length() > 0) {
if (sb.length() > maxSubstring.length()) {
maxSubstring = sb.toString();
}
sb.setLength(0);
} else
continue;
}
if (sb.length() > maxSubstring.length()) {
maxSubstring = sb.toString();
}
return maxSubstring;
}
public static void main(String[] args) {
String s1 = "abcdefgfoo1elt_fooMaxLength";
String s2 = "kpdrefbt!!!01 a werwerwc";
String maxSubstring = maxSubstring(s1, s2);
System.out.println("s1=\"" + s1 + "\"");
System.out.println("s2=\"" + s2 + "\"");
System.out.println("maxSubstring=\"" + maxSubstring + "\"");
System.out.println("-----------------------------------------------------");
s2 = "xaM Length of";
maxSubstring = maxSubstring(s1, s2);
System.out.println("s1=\"" + s1 + "\"");
System.out.println("s2=\"" + s2 + "\"");
System.out.println("maxSubstring=\"" + maxSubstring + "\"");
}
}
c++, implementation
"abcdefghijklmnopqrstuvwxyz", "xcobra" => "abc"
- kyduke November 20, 2015