Posted on

Description

Submission

class Solution {
public:
    bool isPathCrossing(string path) {
        map<char, pair<int, int>> dirs{{'N', {0, 1}}, {'S', {0, -1}}, {'W', {-1, 0}}, {'E', {1, 0}}};
        set<pair<int, int>> visited;
        
        pair<int, int> cur = {0, 0};
        visited.insert(cur);
        
        for(auto c: path) {
            cur.first += dirs[c].first;
            cur.second += dirs[c].second;
            if(visited.find(cur) != visited.end()) return true;
            visited.insert(cur);
        }
        
        return false;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *