Created
September 18, 2020 12:58
-
-
Save marklin-latte/c7d634d47d9cb315a9d4d86ced79d095 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
#include <math.h> | |
#include <limits.h> | |
#include <vector> | |
using namespace std; | |
/** | |
* If the 2nd array is a subset of 1st array, return true. | |
* Time complexity: O(N+M) | |
* M : A length | |
* N : B length | |
* | |
* Space complexity: O(26) | |
* @param chars . | |
*/ | |
bool isSubset(vector<char>& A, vector<char>& B){ | |
vector<int> letters(26); | |
for (char c : A){ | |
// c - 'A' will generate the code as below. | |
// A => 0, B => 1, C = 2 | |
letters[c - 'A']++; | |
} | |
for (char c : B){ | |
if(letters[c - 'A'] == 0){ | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() | |
{ | |
vector<char> a {'A','C','D','D','A'}; | |
vector<char> b {'A','D'}; | |
bool result = isSubset(a,b); // true | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment