Connection Status:
Competition Arena > Submission #19
System Testing
0 / 42
WA 0/42 test cases passed
Submission #19
ProblemAirportCodes
Handlenawa
Submitted2026-07-05 15:23:19
System Messages
Test case #0
Input:    {"FRANKFURT", "ZURICH", "LONDONHEATHROW"}
Expected: {"FRA", "ZRH", "LHR" }
Got:      {"FRA", "ZUR", "LON"}
Source Code
#include <vector>
#include <string>
#include <unordered_set>

using namespace std;

class AirportCodes {
public:
    vector<string> name(vector<string> airports) {
        int n = airports.size();
        // A vector of hash sets to store all 3-letter subsequences for each airport
        vector<unordered_set<string>> subseqs(n);

        // Step 1: Precalculate all unique 3-letter subsequences for each airport
        for (int i = 0; i < n; ++i) {
            string s = airports[i];
            int len = s.length();
            
            for (int c1 = 0; c1 < len - 2; ++c1) {
                for (int c2 = c1 + 1; c2 < len - 1; ++c2) {
                    for (int c3 = c2 + 1; c3 < len; ++c3) {
                        string cand = "";
                        cand += s[c1];
                        cand += s[c2];
                        cand += s[c3];
                        subseqs[i].insert(cand);
                    }
                }
            }
        }

        vector<string> ans(n);
        
        // Step 2: Find a unique code for each airport
        for (int i = 0; i < n; ++i) {
            string s = airports[i];
            int len = s.length();
            string found_code = "";
            
            // Generate combinations in order to pick the first valid one
            for (int c1 = 0; c1 < len - 2 && found_code == ""; ++c1) {
                for (int c2 = c1 + 1; c2 < len - 1 && found_code == ""; ++c2) {
                    for (int c3 = c2 + 1; c3 < len && found_code == ""; ++c3) {
                        string cand = "";
                        cand += s[c1];
                        cand += s[c2];
                        cand += s[c3];
                        
                        bool is_valid = true;
                        
                        // Step 3: Check if this candidate is completely unique to airport 'i'
                        for (int j = 0; j < n; ++j) {
                            if (i == j) continue;
                            
                            // If the candidate exists in another airport's set, it's invalid
                            if (subseqs[j].count(cand)) {
                                is_valid = false;
                                break;
                            }
                        }
                        
                        if (is_valid) {
                            found_code = cand;
                        }
                    }
                }
            }
            
            // Assign the found unique code (or "" if none was found)
            ans[i] = found_code;
        }
        
        return ans;
    }
};