LeetCode Split Concatenated Strings
原题链接在这里: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:
- Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
- 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:
- The input strings will only contain lowercase letters.
- 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的更多相关文章
- [LeetCode] Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- [LeetCode] 555. Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- 【LeetCode】555. Split Concatenated Strings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- 32位JDK和64位JDK
32位和64位系统在计算机领域中常常提及,但是仍然很多人不知道32位和64位的区别,所以本人在网上整理了一些资料,并希望可以与大家一起分享.对于32位和64位之分,本文将分别从处理器,操作系统,JVM ...
- CSS 3 中的多列属性
.column-count <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- linux下字典生成工具-crunch与rtgen
所谓的密码字典主要是配合密码破解软件所使用,密码字典里包括许多人们习惯性设置的密码.这样可以提高密码破解软件的密码破解成功率和命中率,缩短密码破解的时间.当然,如果一个人密码设置没有规律或很复杂,未包 ...
- Spring_属性配置细节
XML 代码: <!-- 使用构造器注入属性值的位置和参数的类型!以区分重载的构造器! --> <bean id="car1" class="com.h ...
- python django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct[转]
1.多表连接查询:当我知道这点的时候顿时觉得django太NX了. class A(models.Model): name = models.CharField(u'名称') clas ...
- truncate delete 与 drop的区别
一张表几亿条数据.根据task_id删除几千万. delete删除后查询,发现查询速度还是没有变快.explain查看 rows并没有发生变化.查询速度肯定也不会变化.原因? truncate del ...
- static变量的作用
在C语言中,关键字static的意思是静态的,有3个明显的作用: 1. 在函数体内,静态变量具有记忆作用,即一个被声明为静态的变量在这一函数被调用的过程中其值维持不变. 2. 在模块内(但在函数体外) ...
- 画龙点睛 之RabbitMQ 初来乍到
消息队列解决了什么问题 1. 异步处理 2. 应用解耦 3. 流量削峰 4. 日志处理...... 安装RabbitMQ (这里的安装我只仅仅说一下linux 的安装 如果想安装windows 版 可 ...
- c# 使用事务
EcAttendanceMatterBLL.OpenTransaction(); DbTransaction Tran = EcAttendanceMatterBLL.Transaction; _wf ...
- ImageView显示图像控件
ImageView显示图像控件 一.简介 1. 2. ImageView,图像视图,直接继承自View类,它的主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用 ...