返回介绍

solution / 1800-1899 / 1842.Next Palindrome Using Same Digits / README_EN

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

1842. Next Palindrome Using Same Digits

中文文档

Description

You are given a numeric string num, representing a very large palindrome.

Return_ the smallest palindrome larger than _num_ that can be created by rearranging its digits. If no such palindrome exists, return an empty string _"".

A palindrome is a number that reads the same backward as forward.

 

Example 1:

Input: num = "1221"
Output: "2112"
Explanation: The next palindrome larger than "1221" is "2112".

Example 2:

Input: num = "32123"
Output: ""
Explanation: No palindromes larger than "32123" can be made by rearranging the digits.

Example 3:

Input: num = "45544554"
Output: "54455445"
Explanation: The next palindrome larger than "45544554" is "54455445".

 

Constraints:

  • 1 <= num.length <= 105
  • num is a palindrome.

Solutions

Solution 1: Find the Next Permutation of the First Half

According to the problem description, we only need to find the next permutation of the first half of the string, then traverse the first half and symmetrically assign values to the second half.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.

class Solution:
  def nextPalindrome(self, num: str) -> str:
    def next_permutation(nums: List[str]) -> bool:
      n = len(nums) // 2
      i = n - 2
      while i >= 0 and nums[i] >= nums[i + 1]:
        i -= 1
      if i < 0:
        return False
      j = n - 1
      while j >= 0 and nums[j] <= nums[i]:
        j -= 1
      nums[i], nums[j] = nums[j], nums[i]
      nums[i + 1 : n] = nums[i + 1 : n][::-1]
      return True

    nums = list(num)
    if not next_permutation(nums):
      return ""
    n = len(nums)
    for i in range(n // 2):
      nums[n - i - 1] = nums[i]
    return "".join(nums)
class Solution {
  public String nextPalindrome(String num) {
    char[] nums = num.toCharArray();
    if (!nextPermutation(nums)) {
      return "";
    }
    int n = nums.length;
    for (int i = 0; i < n / 2; ++i) {
      nums[n - 1 - i] = nums[i];
    }
    return String.valueOf(nums);
  }

  private boolean nextPermutation(char[] nums) {
    int n = nums.length / 2;
    int i = n - 2;
    while (i >= 0 && nums[i] >= nums[i + 1]) {
      --i;
    }
    if (i < 0) {
      return false;
    }
    int j = n - 1;
    while (j >= 0 && nums[i] >= nums[j]) {
      --j;
    }
    swap(nums, i++, j);
    for (j = n - 1; i < j; ++i, --j) {
      swap(nums, i, j);
    }
    return true;
  }

  private void swap(char[] nums, int i, int j) {
    char t = nums[i];
    nums[i] = nums[j];
    nums[j] = t;
  }
}
class Solution {
public:
  string nextPalindrome(string num) {
    int n = num.size();
    string nums = num.substr(0, n / 2);
    if (!next_permutation(begin(nums), end(nums))) {
      return "";
    }
    for (int i = 0; i < n / 2; ++i) {
      num[i] = nums[i];
      num[n - i - 1] = nums[i];
    }
    return num;
  }
};
func nextPalindrome(num string) string {
  nums := []byte(num)
  n := len(nums)
  if !nextPermutation(nums) {
    return ""
  }
  for i := 0; i < n/2; i++ {
    nums[n-1-i] = nums[i]
  }
  return string(nums)
}

func nextPermutation(nums []byte) bool {
  n := len(nums) / 2
  i := n - 2
  for i >= 0 && nums[i] >= nums[i+1] {
    i--
  }
  if i < 0 {
    return false
  }
  j := n - 1
  for j >= 0 && nums[j] <= nums[i] {
    j--
  }
  nums[i], nums[j] = nums[j], nums[i]
  for i, j = i+1, n-1; i < j; i, j = i+1, j-1 {
    nums[i], nums[j] = nums[j], nums[i]
  }
  return true
}
function nextPalindrome(num: string): string {
  const nums = num.split('');
  const n = nums.length;
  if (!nextPermutation(nums)) {
    return '';
  }
  for (let i = 0; i < n >> 1; ++i) {
    nums[n - 1 - i] = nums[i];
  }
  return nums.join('');
}

function nextPermutation(nums: string[]): boolean {
  const n = nums.length >> 1;
  let i = n - 2;
  while (i >= 0 && nums[i] >= nums[i + 1]) {
    i--;
  }
  if (i < 0) {
    return false;
  }
  let j = n - 1;
  while (j >= 0 && nums[i] >= nums[j]) {
    j--;
  }
  [nums[i], nums[j]] = [nums[j], nums[i]];
  for (i = i + 1, j = n - 1; i < j; ++i, --j) {
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  return true;
}

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

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

发布评论

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