返回介绍

solution / 1100-1199 / 1138.Alphabet Board Path / README_EN

发布于 2024-06-17 01:03:23 字数 4980 浏览 0 评论 0 收藏 0

1138. Alphabet Board Path

中文文档

Description

On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"], as shown in the diagram below.

We may make the following moves:

  • 'U' moves our position up one row, if the position exists on the board;
  • 'D' moves our position down one row, if the position exists on the board;
  • 'L' moves our position left one column, if the position exists on the board;
  • 'R' moves our position right one column, if the position exists on the board;
  • '!' adds the character board[r][c] at our current position (r, c) to the answer.

(Here, the only positions that exist on the board are positions with letters on them.)

Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.

 

Example 1:

Input: target = "leet"

Output: "DDR!UURRR!!DDD!"

Example 2:

Input: target = "code"

Output: "RR!DDRR!UUL!R!"

 

Constraints:

  • 1 <= target.length <= 100
  • target consists only of English lowercase letters.

Solutions

Solution 1: Simulation

Starting from the origin point $(0, 0)$, simulate each step of the movement, appending the result of each step to the answer. Note that the direction of movement follows the order "left, up, right, down".

The time complexity is $O(n)$, where $n$ is the length of the string target, as each character in the string target needs to be traversed. Ignoring the space consumption of the answer, the space complexity is $O(1)$.

class Solution:
  def alphabetBoardPath(self, target: str) -> str:
    i = j = 0
    ans = []
    for c in target:
      v = ord(c) - ord("a")
      x, y = v // 5, v % 5
      while j > y:
        j -= 1
        ans.append("L")
      while i > x:
        i -= 1
        ans.append("U")
      while j < y:
        j += 1
        ans.append("R")
      while i < x:
        i += 1
        ans.append("D")
      ans.append("!")
    return "".join(ans)
class Solution {
  public String alphabetBoardPath(String target) {
    StringBuilder ans = new StringBuilder();
    int i = 0, j = 0;
    for (int k = 0; k < target.length(); ++k) {
      int v = target.charAt(k) - 'a';
      int x = v / 5, y = v % 5;
      while (j > y) {
        --j;
        ans.append('L');
      }
      while (i > x) {
        --i;
        ans.append('U');
      }
      while (j < y) {
        ++j;
        ans.append('R');
      }
      while (i < x) {
        ++i;
        ans.append('D');
      }
      ans.append("!");
    }
    return ans.toString();
  }
}
class Solution {
public:
  string alphabetBoardPath(string target) {
    string ans;
    int i = 0, j = 0;
    for (char& c : target) {
      int v = c - 'a';
      int x = v / 5, y = v % 5;
      while (j > y) {
        --j;
        ans += 'L';
      }
      while (i > x) {
        --i;
        ans += 'U';
      }
      while (j < y) {
        ++j;
        ans += 'R';
      }
      while (i < x) {
        ++i;
        ans += 'D';
      }
      ans += '!';
    }
    return ans;
  }
};
func alphabetBoardPath(target string) string {
  ans := []byte{}
  var i, j int
  for _, c := range target {
    v := int(c - 'a')
    x, y := v/5, v%5
    for j > y {
      j--
      ans = append(ans, 'L')
    }
    for i > x {
      i--
      ans = append(ans, 'U')
    }
    for j < y {
      j++
      ans = append(ans, 'R')
    }
    for i < x {
      i++
      ans = append(ans, 'D')
    }
    ans = append(ans, '!')
  }
  return string(ans)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文