leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1138.cpp (758B)


      1 class Solution {
      2     using array_t = array<pair<int, int>, 26>;
      3     static constexpr const array_t lookup = []() constexpr -> array_t {
      4         array_t res;
      5 
      6         for (int i = 0; i < 26; i++)
      7             res[i] = {i / 5, i % 5};
      8 
      9         return res;
     10     }();
     11 
     12   public:
     13     string alphabetBoardPath(const string &target) const {
     14         int x = 0, y = 0;
     15         string res;
     16 
     17         for (const char c : target) {
     18             const auto [i, j] = lookup[c - 'a'];
     19 
     20             if (j < y) res += string(y - j, 'L');
     21             if (i < x) res += string(x - i, 'U');
     22             if (i > x) res += string(i - x, 'D');
     23             if (j > y) res += string(j - y, 'R');
     24 
     25             res += '!';
     26             x = i, y = j;
     27         }
     28 
     29         return res;
     30     }
     31 };