0

I am solving challenges in hackerrank. I came across this question. I am using Dev C++ in my laptop for solving problems and later upload the code into hackerrank. For some test cases, my local Dev C++ IDE showing correct output but hackerrank is showing the wrong output for the same test case.

Code:

#include<bits/stdc++.h> 

using namespace std;

bool ransomeNote(int m, int n, string magazine, string note) {

string temp = ""; int st = 0, end;
bool flag = false;
for (int i = 0; i < note.length(); i++) {
    
    if (note[i] != ' ') {
        temp += note[i];    
    
    } 
    if (note[i] == ' ' || i == note.length() - 1){
        
        
        if ((magazine.find(temp) != string::npos)) {
            
            st = magazine.find(temp);

            magazine.replace(st, temp.length() - 1, "");
            
            flag = true;
            temp = "";
            
            
        } else {
        
            flag = false;
            return flag;
        }
        
    }
}
return flag;    
}


int main() {

int m,n;

cin >> m >> n;

string magazine;
string note;
fflush(stdin);

  
getline(cin, magazine);

getline(cin, note);

if (ransomeNote(m, n, magazine, note)) {

    cout << "Yes";
} else {
    cout << "No";
}


return 0;
}

Output in my IDE :

enter image description here

Hackerrank output : enter image description here

For test cases with output No both are showing the same output but for output Yes they are showing different outputs.

Is there any problem in my code?

Thanks for your help :)

Alan Birtles
  • 22,711
  • 4
  • 22
  • 44
Marivishnu
  • 60
  • 5

0 Answers0