Someone's wrong code was accepted, mine gives TLE (WORDS1)

I recently tried the (medium) problem WORDS1 (Play on Words).


My code:

I tried to solve the problem but I kept getting TLE. My program works for all test cases I have tried till now. So I thought of taking a look at someone else’s solution. Most solutions were badly written (short identifiers, no comments, irreguar indentation). But this one:

seemed good. But after analysing it, It seemed wrong to me. To be sure, I copied the code, compiled it and ran it (without editing).

But that program outputs “The door cannot be opened.” instead of “Ordering is possible.” for the input

bc de ab cd

This is wrong since the input can be reordered to “ab bc cd de”.

How was this submission accepted?

I’m new to data structures and algorithms, so I wouldn’t know if this problem uses a well-known complicated data structure or a difficult algorithm. Please give me some hints. I’ve been trying this problem for 3 days.