原题链接在这里: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. LVS持久化

    在实际应用场景中,轮询调度并不都是适用的.有些情况下,需要我们把同一个会话的请求都调度给一个RS节点.这时候就需要LVS提供持久化的能力,能够实现会话保持. 一.LVS的持久化主要包括以下两个方面. ...

  2. 面试:做过sql优化吗?

    近来面试找工作经常会遇见这种问题: 做过数据库优化吗?大数据量基础过吗?系统反应慢怎么查询? 这咱也没背过啊,面试还老问,现在的网站主要的压力都来自于数据库,频繁的数据库访问经常会使系统瘫痪,这样就需 ...

  3. Spring中的@Transactional以及事务的详细介绍

    首先来说下事务,说到事务就不得不说它的四个特性(acid): 一.特性 1.原子性(atomicity):一个事务当作为一个不可分割的最小工作单元,一组操作要么全部成功,要么全部失败. 2.一致性(c ...

  4. springJDBC的几种方法

    1.简单粗暴,直接在类中创建连接池使用 package com.xiaostudy; import org.apache.commons.dbcp.BasicDataSource; import or ...

  5. protobuf与json转换

    protobuf对象不能直接使用jsonlib去转,因为protobuf生成的对象的get方法返回的类型有byte[],而只有String类型可以作为json的key,protobuf提供方法进行转换 ...

  6. Dynamo论文介绍

    http://catkang.github.io/2016/05/27/dynamo.html Dynamo是Amazon开发的分布式存储系统,本文是阅读Dynamo论文后的总结:Dynamo: Am ...

  7. 布置weblogic10 64位系统

    1.下载64位JDK并安装 2.java -D64 -jar wls1036_generic.jar(注意下载下来的wls1036_generic.jar文件不要解压,用此命令解压) 3.点击下一步, ...

  8. 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".'

    引用:http://www.linuxidc.com/Linux/2015-02/114116.htm Yii2高级版本复制新项目会遇到下面的报错信息: exception 'yii\base\Inv ...

  9. 如何去除pycharm中代码下的波浪线

    Pycharm中新建一个工程,如果不经过配置,在该工程下创建模块写代码,通常是有波浪线的,这样看着很不美观 如何解决这种问题,通常有两种方法 方法一:点击右下角的图标,会出现一个Highlightin ...

  10. 转 IOS动态类型isKindOfClass, isMemberOfClass

    对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) is ...