555. 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.
这道题的意思是给你一串字符串,你需要把所有子字符串连接在一起。在连接的时候,可以选的反转或者不翻转这个子字符串。这时你有一个长的字符串,这叫一个loop,你可以rotate他来找俺字典排序最大的那个长字符串。解题思路分两步:

1、遍历字符串数组,如果反转的字符串大于当前子字符串,把当前子字符串变成反转的字符串。

2、遍历字符串数组,取得当前字符串和当前字符串的反转字符串,分别比较以当前字符串或者当前字符串的反转字符串为rotate节点的长字符串,取最大的。

在第二步的时候可以做优化,如果节点第一个字母小于之前的最大长字符串第一个字母,则可以不比较。代码如下:

public class Solution {
    public String splitLoopedString(String[] strs) {
        for (int i = 0; i < strs.length; i ++) {
            String rev = new StringBuilder(strs[i]).reverse().toString();
            if (strs[i].compareTo(rev) < 0) {
                strs[i] = rev;
            }
        }
        String res = "a";
        for (int i = 0; i < strs.length; i ++) {
            String rev = new StringBuilder(strs[i]).reverse().toString();
            for (String str2: new String[]{strs[i], rev}) {
                for (int k = 0; k < str2.length(); k ++) {
                    if (str2.charAt(k) >= res.charAt(0)) { //optimize OJ time from 200ms to 96ms
                        StringBuilder sb = new StringBuilder(str2.substring(k));
                        for (int j = i + 1; j < strs.length; j ++) {
                            sb.append(strs[j]);
                        }
                        for (int j = 0; j < i; j ++) {
                            sb.append(strs[j]);
                        }
                        sb.append(str2.substring(0, k));
                        if (res.compareTo(sb.toString()) < 0){
                            res = sb.toString();
                        }
                    }
                }
            }
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值