Last active
December 18, 2015 12:39
-
-
Save ygabo/5784709 to your computer and use it in GitHub Desktop.
Ransom note
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 <map> | |
using namespace std; | |
bool ransom( string note, string mag ){ | |
if( mag.empty() ) return false; | |
if( note.empty()) return true; | |
int length = note.length(); | |
int length_mag = mag.length(); | |
if( length > length_mag ) return false; | |
map<char, int> note_m; | |
for( int i = 0; i < length; ++i){ | |
note_m[note[i]]++; | |
} | |
for( int i = 0; i < length_mag; ++i){ | |
if( note_m[mag[i]] > 0 ){ | |
note_m[mag[i]]--; | |
length--; | |
} | |
if( length <= 0 ) return true; | |
} | |
if( length <= 0 ) return true; | |
else | |
return false; | |
} | |
void main(){ | |
string s = "hello"; | |
string mag = "dsfhsfhjskfhelerereo"; | |
if( ransom(s, mag) ) cout << "YES" <<endl; | |
else | |
cout << "NOP" << endl; | |
cin.get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment