Write an efficient C function that takes two strings as arguments and removes the characters from first string which are present in second string (mask string).
Key Point: char[256], hashtable, use space for time trade-off.
From Coding Interviews: Questions, Analysis & Solutions
void DeleteCharacters(char* pString, char* pCharsToBeDeleted) {
int hashTable[256];
const char* pTemp = pCharsToBeDeleted;
char* pSlow = pString;
char* pFast = pString;
if(pString == NULL || pCharsToBeDeleted == NULL)
return;
memset(hashTable, 0, sizeof(hashTable));
while (*pTemp != '\0') {
hashTable[*pTemp] = 1;
++ pTemp;
}
while (*pFast != '\0') {
if(hashTable[*pFast] != 1) {
*pSlow = *pFast;
++ pSlow;
}
++pFast;
}
*pSlow = '\0';
}
Read full article from Remove characters from the first string which are present in the second string | GeeksforGeeks
Key Point: char[256], hashtable, use space for time trade-off.
From Coding Interviews: Questions, Analysis & Solutions
void DeleteCharacters(char* pString, char* pCharsToBeDeleted) {
int hashTable[256];
const char* pTemp = pCharsToBeDeleted;
char* pSlow = pString;
char* pFast = pString;
if(pString == NULL || pCharsToBeDeleted == NULL)
return;
memset(hashTable, 0, sizeof(hashTable));
while (*pTemp != '\0') {
hashTable[*pTemp] = 1;
++ pTemp;
}
while (*pFast != '\0') {
if(hashTable[*pFast] != 1) {
*pSlow = *pFast;
++ pSlow;
}
++pFast;
}
*pSlow = '\0';
}
Read full article from Remove characters from the first string which are present in the second string | GeeksforGeeks