返回介绍

solution / 0200-0299 / 0214.Shortest Palindrome / README_EN

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

214. Shortest Palindrome

中文文档

Description

You are given a string s. You can convert s to a palindrome by adding characters in front of it.

Return _the shortest palindrome you can find by performing this transformation_.

 

Example 1:

Input: s = "aacecaaa"
Output: "aaacecaaa"

Example 2:

Input: s = "abcd"
Output: "dcbabcd"

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of lowercase English letters only.

Solutions

Solution 1

class Solution:
  def shortestPalindrome(self, s: str) -> str:
    base = 131
    mod = 10**9 + 7
    n = len(s)
    prefix = suffix = 0
    mul = 1
    idx = 0
    for i, c in enumerate(s):
      prefix = (prefix * base + (ord(c) - ord('a') + 1)) % mod
      suffix = (suffix + (ord(c) - ord('a') + 1) * mul) % mod
      mul = (mul * base) % mod
      if prefix == suffix:
        idx = i + 1
    return s if idx == n else s[idx:][::-1] + s
class Solution {
  public String shortestPalindrome(String s) {
    int base = 131;
    int mul = 1;
    int mod = (int) 1e9 + 7;
    int prefix = 0, suffix = 0;
    int idx = 0;
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      int t = s.charAt(i) - 'a' + 1;
      prefix = (int) (((long) prefix * base + t) % mod);
      suffix = (int) ((suffix + (long) t * mul) % mod);
      mul = (int) (((long) mul * base) % mod);
      if (prefix == suffix) {
        idx = i + 1;
      }
    }
    if (idx == n) {
      return s;
    }
    return new StringBuilder(s.substring(idx)).reverse().toString() + s;
  }
}
typedef unsigned long long ull;

class Solution {
public:
  string shortestPalindrome(string s) {
    int base = 131;
    ull mul = 1;
    ull prefix = 0;
    ull suffix = 0;
    int idx = 0, n = s.size();
    for (int i = 0; i < n; ++i) {
      int t = s[i] - 'a' + 1;
      prefix = prefix * base + t;
      suffix = suffix + mul * t;
      mul *= base;
      if (prefix == suffix) idx = i + 1;
    }
    if (idx == n) return s;
    string x = s.substr(idx, n - idx);
    reverse(x.begin(), x.end());
    return x + s;
  }
};
func shortestPalindrome(s string) string {
  n := len(s)
  base, mod := 131, int(1e9)+7
  prefix, suffix, mul := 0, 0, 1
  idx := 0
  for i, c := range s {
    t := int(c-'a') + 1
    prefix = (prefix*base + t) % mod
    suffix = (suffix + t*mul) % mod
    mul = (mul * base) % mod
    if prefix == suffix {
      idx = i + 1
    }
  }
  if idx == n {
    return s
  }
  x := []byte(s[idx:])
  for i, j := 0, len(x)-1; i < j; i, j = i+1, j-1 {
    x[i], x[j] = x[j], x[i]
  }
  return string(x) + s
}
impl Solution {
  pub fn shortest_palindrome(s: String) -> String {
    let base = 131;
    let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1);
    for (i, c) in s.chars().enumerate() {
      let t = (c as u64) - ('0' as u64) + 1;
      prefix = prefix * base + t;
      suffix = suffix + t * mul;
      mul *= base;
      if prefix == suffix {
        idx = i + 1;
      }
    }
    if idx == s.len() {
      s
    } else {
      let x: String = (&s[idx..]).chars().rev().collect();
      String::from(x + &s)
    }
  }
}
// https://leetcode.com/problems/shortest-palindrome/

using System.Text;

public partial class Solution
{
  public string ShortestPalindrome(string s)
  {
    for (var i = s.Length - 1; i >= 0; --i)
    {
      var k = i;
      var j = 0;
      while (j < k)
      {
        if (s[j] == s[k])
        {
          ++j;
          --k;
        }
        else
        {
          break;
        }
      }
      if (j >= k)
      {
        var sb = new StringBuilder(s.Length * 2 - i - 1);
        for (var l = s.Length - 1; l >= i + 1; --l)
        {
          sb.Append(s[l]);
        }
        sb.Append(s);
        return sb.ToString();
      }
    }

    return string.Empty;
  }
}

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

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

发布评论

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