返回介绍

solution / 1500-1599 / 1585.Check If String Is Transformable With Substring Sort Operations / README_EN

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

1585. Check If String Is Transformable With Substring Sort Operations

中文文档

Description

Given two strings s and t, transform string s into string t using the following operation any number of times:

  • Choose a non-empty substring in s and sort it in place so the characters are in ascending order.
    • For example, applying the operation on the underlined substring in "14234" results in "12344".

Return true if _it is possible to transform s into t_. Otherwise, return false.

A substring is a contiguous sequence of characters within a string.

 

Example 1:

Input: s = "84532", t = "34852"
Output: true
Explanation: You can transform s into t using the following sort operations:
"84532" (from index 2 to 3) -> "84352"
"84352" (from index 0 to 2) -> "34852"

Example 2:

Input: s = "34521", t = "23415"
Output: true
Explanation: You can transform s into t using the following sort operations:
"34521" -> "23451"
"23451" -> "23415"

Example 3:

Input: s = "12345", t = "12435"
Output: false

 

Constraints:

  • s.length == t.length
  • 1 <= s.length <= 105
  • s and t consist of only digits.

Solutions

Solution 1

class Solution:
  def isTransformable(self, s: str, t: str) -> bool:
    pos = defaultdict(deque)
    for i, c in enumerate(s):
      pos[int(c)].append(i)
    for c in t:
      x = int(c)
      if not pos[x] or any(pos[i] and pos[i][0] < pos[x][0] for i in range(x)):
        return False
      pos[x].popleft()
    return True
class Solution {
  public boolean isTransformable(String s, String t) {
    Deque<Integer>[] pos = new Deque[10];
    Arrays.setAll(pos, k -> new ArrayDeque<>());
    for (int i = 0; i < s.length(); ++i) {
      pos[s.charAt(i) - '0'].offer(i);
    }
    for (int i = 0; i < t.length(); ++i) {
      int x = t.charAt(i) - '0';
      if (pos[x].isEmpty()) {
        return false;
      }
      for (int j = 0; j < x; ++j) {
        if (!pos[j].isEmpty() && pos[j].peek() < pos[x].peek()) {
          return false;
        }
      }
      pos[x].poll();
    }
    return true;
  }
}
class Solution {
public:
  bool isTransformable(string s, string t) {
    queue<int> pos[10];
    for (int i = 0; i < s.size(); ++i) {
      pos[s[i] - '0'].push(i);
    }
    for (char& c : t) {
      int x = c - '0';
      if (pos[x].empty()) {
        return false;
      }
      for (int j = 0; j < x; ++j) {
        if (!pos[j].empty() && pos[j].front() < pos[x].front()) {
          return false;
        }
      }
      pos[x].pop();
    }
    return true;
  }
};
func isTransformable(s string, t string) bool {
  pos := [10][]int{}
  for i, c := range s {
    pos[c-'0'] = append(pos[c-'0'], i)
  }
  for _, c := range t {
    x := int(c - '0')
    if len(pos[x]) == 0 {
      return false
    }
    for j := 0; j < x; j++ {
      if len(pos[j]) > 0 && pos[j][0] < pos[x][0] {
        return false
      }
    }
    pos[x] = pos[x][1:]
  }
  return true
}

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

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

发布评论

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