返回介绍

solution / 0500-0599 / 0555.Split Concatenated Strings / README_EN

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

555. Split Concatenated Strings

中文文档

Description

You are given an array of strings strs. You could concatenate these strings together into a loop, where for each string, you could choose to reverse it or not. Among all the possible loops

Return _the lexicographically largest string after cutting the loop, which will make the looped string into a regular one_.

Specifically, to find the lexicographically largest string, you need to experience two phases:

  1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
  2. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from the character at the cutpoint.

And your job is to find the lexicographically largest one among all the possible regular strings.

 

Example 1:

Input: strs = ["abc","xyz"]
Output: "zyxcba"
Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", where '-' represents the looped status. 
The answer string came from the fourth looped one, where you could cut from the middle character 'a' and get "zyxcba".

Example 2:

Input: strs = ["abc"]
Output: "cba"

 

Constraints:

  • 1 <= strs.length <= 1000
  • 1 <= strs[i].length <= 1000
  • 1 <= sum(strs[i].length) <= 1000
  • strs[i] consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def splitLoopedString(self, strs: List[str]) -> str:
    strs = [s[::-1] if s[::-1] > s else s for s in strs]
    ans = ''.join(strs)
    for i, s in enumerate(strs):
      t = ''.join(strs[i + 1 :]) + ''.join(strs[:i])
      for j in range(len(s)):
        a = s[j:]
        b = s[:j]
        ans = max(ans, a + t + b)
        ans = max(ans, b[::-1] + t + a[::-1])
    return ans
class Solution {
  public String splitLoopedString(String[] strs) {
    int n = strs.length;
    for (int i = 0; i < n; ++i) {
      String s = strs[i];
      String t = new StringBuilder(s).reverse().toString();
      if (s.compareTo(t) < 0) {
        strs[i] = t;
      }
    }
    String ans = "";
    for (int i = 0; i < n; ++i) {
      String s = strs[i];
      StringBuilder sb = new StringBuilder();
      for (int j = i + 1; j < n; ++j) {
        sb.append(strs[j]);
      }
      for (int j = 0; j < i; ++j) {
        sb.append(strs[j]);
      }
      String t = sb.toString();
      for (int j = 0; j < s.length(); ++j) {
        String a = s.substring(j);
        String b = s.substring(0, j);
        String cur = a + t + b;
        if (ans.compareTo(cur) < 0) {
          ans = cur;
        }
        cur = new StringBuilder(b)
              .reverse()
              .append(t)
              .append(new StringBuilder(a).reverse().toString())
              .toString();
        if (ans.compareTo(cur) < 0) {
          ans = cur;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  string splitLoopedString(vector<string>& strs) {
    for (auto& s : strs) {
      string t{s.rbegin(), s.rend()};
      s = max(s, t);
    }
    int n = strs.size();
    string ans = "";
    for (int i = 0; i < strs.size(); ++i) {
      auto& s = strs[i];
      string t;
      for (int j = i + 1; j < n; ++j) {
        t += strs[j];
      }
      for (int j = 0; j < i; ++j) {
        t += strs[j];
      }
      for (int j = 0; j < s.size(); ++j) {
        auto a = s.substr(j);
        auto b = s.substr(0, j);
        auto cur = a + t + b;
        if (ans < cur) {
          ans = cur;
        }
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        cur = b + t + a;
        if (ans < cur) {
          ans = cur;
        }
      }
    }
    return ans;
  }
};
func splitLoopedString(strs []string) (ans string) {
  for i, s := range strs {
    t := reverse(s)
    if s < t {
      strs[i] = t
    }
  }
  for i, s := range strs {
    sb := &strings.Builder{}
    for _, w := range strs[i+1:] {
      sb.WriteString(w)
    }
    for _, w := range strs[:i] {
      sb.WriteString(w)
    }
    t := sb.String()
    for j := 0; j < len(s); j++ {
      a, b := s[j:], s[0:j]
      cur := a + t + b
      if ans < cur {
        ans = cur
      }
      cur = reverse(b) + t + reverse(a)
      if ans < cur {
        ans = cur
      }
    }
  }
  return ans
}

func reverse(s string) string {
  t := []byte(s)
  for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
    t[i], t[j] = t[j], t[i]
  }
  return string(t)
}

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

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

发布评论

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