[LeetCode] Split Concatenated Strings 分割串联字符串

简介:

Given a list of strings, 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, you need to find the lexicographically biggest string after cutting the loop, which will make the looped string into a regular one.

Specifically, to find the lexicographically biggest 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 biggest one among all the possible regular strings.

Example:

Input: "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".

Note:

  1. The input strings will only contain lowercase letters.
  2. The total length of all the strings will not over 1,000. 

这道题给了我们一些字符串,让我们将其连接起来,连接的时候对于每个字符串我们可以选择翻转或者不翻转,在行程的大的字符串上找一个位置cut掉,将该位置当作首字符,前面的字符串移动到末尾去,问怎么cut能使字符串的字母顺序大。刚开始博主想,既然要让最终字符串字母顺序最大,那么每一个字符串当然要尽可能的大了,所以如果其翻转字符串的字母顺序大的话,就要对字符串进行翻转。然后在组成的字符串中找最大的字符进行cut,然而这种思路不一定能得到正确的结果。比如字符串数组["lc", "love", "ydc"],如果按照博主之前的思路得到的字符串应该为"ydclclove",但正确结果应该是"ylclovecd"。我们可出来正确的答案中cut位置所在的字符串ydc,虽然cdy小于ydc,但还是翻转了。但是其他的字符都是按照字母顺序来确定要不要翻转的,那么我们可以得出这样的结论,只有cut所在的字符串的翻转可能不按规律。那么我们如何确定cut位置呢,其实没有太好的办法,只能遍历每一个字母。我们首先来根据字母顺序确定要不要翻转每一个字符串,将字母顺序大的连成一个字符串,然后遍历每一个字符串,在每一个字符串中遍历每一个位置,将当前遍历的字符串后面的所有字符串跟前面所有字符串先连起来,存入mid中,然后取出当前遍历的字符串中当前遍历的位置及其后面的字符取出,连上mid,然后再连上当前位置前面的字符,然后跟结果res比较,取较大者存入结果res。这里我们可以进行小优化,如果cut位置的字母大于等于结果res的首字母,我们才进行对比更新。注意我们在遍历每个字符串时,要将其翻转字符串的每一位也遍历了,这样才能涵盖所有的情况,参见代码如下:

public:
    string splitLoopedString(vector<string>& strs) {
        if (strs.empty()) return "";
        string s = "", res = "a";
        int n = strs.size(), cur = 0;
        for (string str : strs) {
            string t = string(str.rbegin(), str.rend());
            s += str > t ? str : t;
        }
        for (int i = 0; i < n; ++i) {          
            string t1 = strs[i], t2 = string(t1.rbegin(), t1.rend());
            string mid = s.substr(cur + t1.size()) + s.substr(0, cur);
            for (int j = 0; j < strs[i].size(); ++j) {
                if (t1[j] >= res[0]) res = max(res, t1.substr(j) + mid + t1.substr(0, j));
                if (t2[j] >= res[0]) res = max(res, t2.substr(j) + mid + t2.substr(0, j));
            }
            cur += strs[i].size();
        }
        return res;
    }
}; 

参考资料:

https://discuss.leetcode.com/topic/86545/c-9ms-12-lines/2

https://discuss.leetcode.com/topic/86509/easy-understanding-c-solution-with-detailed-explnation

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Split Concatenated Strings 分割串联字符串

,如需转载请自行联系原博主。

相关文章
|
6月前
|
Go
【LeetCode 热题100】DP 实战进阶:最长递增子序列、乘积最大子数组、分割等和子集(力扣300 / 152/ 416 )(Go语言版)
本文深入解析三道经典的动态规划问题:**最长递增子序列(LIS)**、**乘积最大子数组** 和 **分割等和子集**。 - **300. LIS** 通过 `dp[i]` 表示以第 `i` 个元素结尾的最长递增子序列长度,支持 O(n²) 动态规划与 O(n log n) 的二分优化。 - **152. 乘积最大子数组** 利用正负数特性,同时维护最大值与最小值的状态转移方程。 - **416. 分割等和子集** 转化为 0-1 背包问题,通过布尔型 DP 实现子集和判断。 总结对比了三题的状态定义与解法技巧,并延伸至相关变种问题,助你掌握动态规划的核心思想与灵活应用!
251 1
|
8月前
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
207 6
|
9月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
287 11
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
120 9
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
161 1
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
175 0
【LeetCode 22】459.重复的子字符串
【LeetCode 22】459.重复的子字符串
134 0
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
117 0
【LeetCode 19】541.反转字符串II
【LeetCode 19】541.反转字符串II
103 0
【LeetCode 18】6.2.反转字符串
【LeetCode 18】6.2.反转字符串
90 0

热门文章

最新文章