[Codewars] Directions Reduction

Directions Reduction

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using namespace std;
class DirReduction
{
public:
static std::vector<std::string> dirReduc(std::vector<std::string> &arr) {
unordered_map<string, string> appo;
appo["NORTH"] = "SOUTH";
appo["SOUTH"] = "NORTH";
appo["EAST"] = "WEST";
appo["WEST"] = "EAST";
vector<string> res;
for(auto d : arr) {
if(res.size() and res.back() == appo[d]) res.pop_back();
else res.push_back(d);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/31/PS/Codewars/directions-reduction/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.