Microsoft interview question

He asked me to write a function to detect whether string1 contains all letters in string2

Interview Answers

Anonymous

15 Oct 2014

Traverse string2 and create a map where the characters of string2 are used as keys. Then traverse string1. If the the character in string1 exists in the map, remove the key from the map. At the end if your map has anything in it, string1 did not contain all of the characters.

3

Anonymous

25 Jan 2019

def hasAllLetters(str1,str2): if str2 == "": return True else: dict = {} for ch2 in str2: if ch2 in dict: dict[ch2] += 1 else: dict[ch2] = 1 for ch1 in str1: if ch1 in dict: if dict[ch1] > 0: dict[ch1] -= 1 else: del dict[ch1] else: return False return True

1

Anonymous

10 Oct 2013

// Assume S1 and S2 are non-null pointers bool hasChar(char *s1, char *s2) { int ret=false; char *tmp; if (*s2 == '\0') return true; tmp = s1; while ((*tmp != '\0') && (*tmp != *s2)) tmp++ if (*tmp == '\0') return false; return hasChar(s1, s2++); }

Anonymous

4 Nov 2018

private static bool isEqual(string s1, string s2) { int[] first = new int[26]; int[] second = new int[26]; for (int i = 0; i 0) return false; } return true; }