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 ...
随机推荐
- WINDOWS和UNIX换行符的理解
# WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37 其他 美国54.163.255.40 其他 美国 弗吉尼亚州 亚马逊公司 **[ro ...
- PHP memcache扩展模块安装
安装php扩展模块memcache memcache 的工作就是在专门的机器的内存里维护一张巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率,减轻后端数据库的读写压力. ...
- 一种BIM缺失多态性介导的酪氨酸激酶抑制剂的耐药性
论文名称:A common BIM deletion polymorphism mediates intrinsic resistance and inferior responses to tyro ...
- SQLMAP 使用手册
当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些数据 sqlmap支持五种不同的注入模式: ...
- Effective C++ 条款05:了解C++编写并调用哪些函数
规则一 编译器默认操作 // 你认为 class Empty { }; // 实际上 class Empty { public: Empty() { ... } // default 构造函数 Emp ...
- quartz(6)--集群
Quartz应用能被集群,是水平集群还是垂直集群取决于你自己的需要.集群提供以下好处: · 伸缩性 · 高可用性 · 负载均衡 目前,Quartz只能借助关系数据库和JDBC作业存储支持集群. qua ...
- DPDK l2fwd
dpdk的l2fwd主要做二层转发,代码分析如下. #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- Permutations,全排列
问题描述:给定一个数组,数字中数字不重复,求所有全排列. 算法分析:可以用交换递归法,也可以用插入法. 递归法:例如,123,先把1和1交换,然后递归全排列2和3,然后再把1和1换回来.1和2交换,全 ...
- scala学习手记30 - 闭包
首先要弄白闭包的概念. 教材中的说法是:闭包是一种特殊的函数值,闭包中封闭或绑定了在另一个作用域或上下文中定义的变量.这里说闭包是一种特殊的函数值. 维基百科中的说法是:在计算机科学中,闭包(英语:C ...
- mysql应该看的blog
一个比较系统的学习mysql的网站:http://www.runoob.com/mysql/mysql-data-types.html