返回介绍

solution / 1900-1999 / 1946.Largest Number After Mutating Substring / README_EN

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

1946. Largest Number After Mutating Substring

中文文档

Description

You are given a string num, which represents a large integer. You are also given a 0-indexed integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d maps to digit change[d].

You may choose to mutate a single substring of num. To mutate a substring, replace each digit num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]).

Return _a string representing the largest possible integer after mutating (or choosing not to) a single substring of _num.

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

 

Example 1:

Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8]
Output: "832"
Explanation: Replace the substring "1":
- 1 maps to change[1] = 8.
Thus, "132" becomes "832".
"832" is the largest number that can be created, so return it.

Example 2:

Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6]
Output: "934"
Explanation: Replace the substring "021":
- 0 maps to change[0] = 9.
- 2 maps to change[2] = 3.
- 1 maps to change[1] = 4.
Thus, "021" becomes "934".
"934" is the largest number that can be created, so return it.

Example 3:

Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4]
Output: "5"
Explanation: "5" is already the largest number that can be created, so return it.

 

Constraints:

  • 1 <= num.length <= 105
  • num consists of only digits 0-9.
  • change.length == 10
  • 0 <= change[d] <= 9

Solutions

Solution 1

class Solution:
  def maximumNumber(self, num: str, change: List[int]) -> str:
    s = list(num)
    for i, c in enumerate(s):
      if change[int(c)] > int(c):
        while i < len(s) and int(s[i]) <= change[int(s[i])]:
          s[i] = str(change[int(s[i])])
          i += 1
        break
    return ''.join(s)
class Solution {
  public String maximumNumber(String num, int[] change) {
    char[] s = num.toCharArray();
    for (int i = 0; i < s.length; ++i) {
      if (change[s[i] - '0'] > s[i] - '0') {
        for (; i < s.length && s[i] - '0' <= change[s[i] - '0']; ++i) {
          s[i] = (char) (change[s[i] - '0'] + '0');
        }
        break;
      }
    }
    return String.valueOf(s);
  }
}
class Solution {
public:
  string maximumNumber(string num, vector<int>& change) {
    int n = num.size();
    for (int i = 0; i < n; ++i) {
      if (change[num[i] - '0'] > num[i] - '0') {
        for (; i < n && change[num[i] - '0'] >= num[i] - '0'; ++i) {
          num[i] = change[num[i] - '0'] + '0';
        }
        break;
      }
    }
    return num;
  }
};
func maximumNumber(num string, change []int) string {
  s := []byte(num)
  for i, c := range num {
    if change[c-'0'] > int(c-'0') {
      for ; i < len(s) && change[s[i]-'0'] >= int(s[i]-'0'); i++ {
        s[i] = byte(change[s[i]-'0']) + '0'
      }
      break
    }
  }
  return string(s)
}

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

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

发布评论

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