Skip to content

Instantly share code, notes, and snippets.

@ygabo
Last active December 18, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygabo/5784709 to your computer and use it in GitHub Desktop.
Save ygabo/5784709 to your computer and use it in GitHub Desktop.
Ransom note
#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