返回介绍

solution / 1400-1499 / 1417.Reformat The String / README_EN

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

1417. Reformat The String

中文文档

Description

You are given an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).

You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.

Return _the reformatted string_ or return an empty string if it is impossible to reformat the string.

 

Example 1:

Input: s = "a0b1c2"
Output: "0a1b2c"
Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.

Example 2:

Input: s = "leetcode"
Output: ""
Explanation: "leetcode" has only characters so we cannot separate them by digits.

Example 3:

Input: s = "1229857369"
Output: ""
Explanation: "1229857369" has only digits so we cannot separate them by characters.

 

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters and/or digits.

Solutions

Solution 1

class Solution:
  def reformat(self, s: str) -> str:
    a = [c for c in s if c.islower()]
    b = [c for c in s if c.isdigit()]
    if abs(len(a) - len(b)) > 1:
      return ''
    if len(a) < len(b):
      a, b = b, a
    ans = []
    for x, y in zip(a, b):
      ans.append(x + y)
    if len(a) > len(b):
      ans.append(a[-1])
    return ''.join(ans)
class Solution {
  public String reformat(String s) {
    StringBuilder a = new StringBuilder();
    StringBuilder b = new StringBuilder();
    for (char c : s.toCharArray()) {
      if (Character.isDigit(c)) {
        a.append(c);
      } else {
        b.append(c);
      }
    }
    int m = a.length(), n = b.length();
    if (Math.abs(m - n) > 1) {
      return "";
    }
    StringBuilder ans = new StringBuilder();
    for (int i = 0; i < Math.min(m, n); ++i) {
      if (m > n) {
        ans.append(a.charAt(i));
        ans.append(b.charAt(i));
      } else {
        ans.append(b.charAt(i));
        ans.append(a.charAt(i));
      }
    }
    if (m > n) {
      ans.append(a.charAt(m - 1));
    }
    if (m < n) {
      ans.append(b.charAt(n - 1));
    }
    return ans.toString();
  }
}
class Solution {
public:
  string reformat(string s) {
    string a = "", b = "";
    for (char c : s) {
      if (isdigit(c))
        a += c;
      else
        b += c;
    }
    int m = a.size(), n = b.size();
    if (abs(m - n) > 1) return "";
    string ans = "";
    for (int i = 0; i < min(m, n); ++i) {
      if (m > n) {
        ans += a[i];
        ans += b[i];
      } else {
        ans += b[i];
        ans += a[i];
      }
    }
    if (m > n) ans += a[m - 1];
    if (m < n) ans += b[n - 1];
    return ans;
  }
};
func reformat(s string) string {
  a := []byte{}
  b := []byte{}
  for _, c := range s {
    if unicode.IsLetter(c) {
      a = append(a, byte(c))
    } else {
      b = append(b, byte(c))
    }
  }
  if len(a) < len(b) {
    a, b = b, a
  }
  if len(a)-len(b) > 1 {
    return ""
  }
  var ans strings.Builder
  for i := range b {
    ans.WriteByte(a[i])
    ans.WriteByte(b[i])
  }
  if len(a) > len(b) {
    ans.WriteByte(a[len(a)-1])
  }
  return ans.String()
}

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

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

发布评论

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