原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/

题目:

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.

题解:

每个词或正或反连城环,在中间切一刀变成线,找字母顺序最大的线.

除了切开那点所在字符串s之外,其他的字符串都是自己排成字母顺序最大.

切开的那个字符串 或正或反都有可能. 在或正或反的每一个位置切开连上看得到的结果是否更大维护最大值给res.

Time Complexity: O(k*n^2). k是字符串的平均长度. n = strs.length.

Space: O(k*n).

AC Java:

 class Solution {
public String splitLoopedString(String[] strs) {
for(int i = 0; i<strs.length; i++){
String reverse = new StringBuilder(strs[i]).reverse().toString();
if(strs[i].compareTo(reverse) < 0){
strs[i] = reverse;
}
} int len = strs.length;
String res = "";
for(int i = 0; i<len; i++){
String reverse = new StringBuilder(strs[i]).reverse().toString();
for(String s : new String[]{strs[i], reverse}){
for(int k = 0; k<s.length(); k++){
StringBuilder sb = new StringBuilder(s.substring(k)); for(int j = i+1; j<len; j++){
sb.append(strs[j]);
}
for(int j = 0; j<i; j++){
sb.append(strs[j]);
} sb.append(s.substring(0, k));
if(sb.toString().compareTo(res) > 0){
res = sb.toString();
}
}
}
}
return res;
}
}

LeetCode Split Concatenated Strings的更多相关文章

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

    Given a list of strings, you could concatenate these strings together into a loop, where for each st ...

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

    Given a list of strings, you could concatenate these strings together into a loop, where for each st ...

  3. 【LeetCode】555. Split Concatenated Strings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  6. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  7. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  9. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

随机推荐

  1. Runnable、Callable

    Runnable 任务,没有返回值 Callable 任务,又返回值 Runnable与Callable 相同点: 1. 都是接口: 2. 用来编写多线程程序: 3. 都需要调用Thread.star ...

  2. 20145201 《Java程序设计》第五周学习总结

    20145201 <Java程序设计>第五周学习总结 教材学习内容总结 本周学习了课本第八.九章内容,即异常处理.Collection与Map. 第八章 异常处理 8.1 语法与集成架构 ...

  3. 20145109 《Java程序设计》第八周学习总结

    Chapter 15 API java.util.logging package The constructor of Logger class is protected. If Logger ins ...

  4. Autofac Getting Started(默认的构造函数注入)

    https://autofaccn.readthedocs.io/en/latest/getting-started/index.html The basic pattern for integrat ...

  5. 基于 CodeIgniter 的各类开源项目大全

    名称:STBlog 介绍:STBlog 是一套由CI中国社区驱动,基于Codeigniter MVC 框架编写的多权限博客系统,轻巧/快速/安全/易拓展/界面友好是它的最大特点. 官方:http:// ...

  6. LeetCode——Next Greater Element I

    LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...

  7. Spring中<ref local=""/>与<ref bean=""/>区别

    小 Spring中<ref local=""/>与<ref bean=""/>区别 (2011-03-19 19:21:58) 转载▼ ...

  8. VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展

    1.安装虚拟机后,检查C盘容量大小,发现C盘现在的空间是59.9GB,如下图: 2.使用window+R键,出现运行窗口,输入‘cmd’——>‘cd C:\Program Files (x86) ...

  9. C语言排序算法之简单交换法排序,直接选择排序,冒泡排序

    C语言排序算法之简单交换法排序,直接选择排序,冒泡排序,最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 简单交换法排序 /*简单交换法排序 根据序列中两个记录键值的比较结果来对换这两 ...

  10. 图片加载之Picasso使用

    简介 Picasso是Square公司开源的一个Android图形缓存库,可以实现图片下载和缓存功能. 主要有以下一些特性: 在Adapter中回收和取消已经不在视野范围图片资源的加载,防止可能出现的 ...