Java--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:
- 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.
package com.qa.qf; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Solution { /** * 率选单个字符串如何分割可以得到最大字串 * @param str * @return */ public Map<String, Object> maxOfString(String str) { int len = str.length(); String max = ""; String rl = ""; int k = 0; boolean isLeft = false; for (int i = 0; i < len; i++) { String r = str.substring(i, len); String l = new StringBuilder(str.substring(0,i+1)).reverse().toString(); if(r.compareTo(l) > 0){ rl = r; }else { rl = l; isLeft = true; } if(rl.compareTo(max) > 0){ max = rl; k = i; } } Map<String, Object> map = new HashMap<>(2); //分割位置 map.put("k", k); //最大字串 map.put("max", max); //左边大还是右边大 map.put("isLeft", isLeft); return map; } public String maxOfStrings(List<String> strs){ int index = 0; String max = ""; int m = 0; boolean isLeft = false; for(int i=0; i<strs.size(); i++){ Map map = maxOfString(strs.get(i)); if(map.get("max").toString().compareTo(max)>0){ max = map.get("max").toString(); m = Integer.valueOf(map.get("k").toString()); index = i; isLeft = Boolean.valueOf(map.get("isLeft").toString()); } } StringBuilder sb = new StringBuilder(); String pre = ""; String suf = ""; if(isLeft){ pre = new StringBuilder(strs.get(index).substring(0,m+1)).reverse().toString(); suf = new StringBuilder(strs.get(index).substring(m+1)).reverse().toString(); }else { pre = strs.get(index).substring(m); suf = strs.get(index).substring(0,m); } sb.append(pre); for(int j=index+1; j<strs.size(); j++){ String str1 = strs.get(j); String str2 = new StringBuilder(strs.get(j)).reverse().toString(); sb = str1.compareTo(str2) > 0 ? sb.append(str1) : sb.append(str2); } for(int k=0; k<index; k++){ String str1 = strs.get(k); String str2 = new StringBuilder(strs.get(k)).reverse().toString(); sb = str1.compareTo(str2) > 0 ? sb.append(str1) : sb.append(str2); } return sb.append(suf).toString(); } public static void main(String[] args){ Solution solution = new Solution(); String str1 = "abc"; String str2 = "xyz"; String str3 = "hijxyzyx"; List<String> list = new ArrayList<>(); list.add(str1); list.add(str2); list.add(str3); list.add(str3); System.out.println(solution.maxOfStrings(list)); } }