• 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:

    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.
    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));
        }
    
    }
  • 相关阅读:
    Java自学第8期——多线程
    Java自学第7期——异常(Exception)
    Java自学第6期——Collection、Map、迭代器、泛型、可变参数、集合工具类、集合数据结构、Debug
    java自学第5期——Object、Date、Calender、System、StringBuilder、基本类型包装类
    下载com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    博客园主题SimpleMemory详细配置项
    banner自用图床
    java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
    java自学第3期——继承、多态、接口、抽象类、final关键字、权限修饰符、内部类
    数组,哈希表,字典
  • 原文地址:https://www.cnblogs.com/fqfanqi/p/12186411.html
Copyright © 2020-2023  润新知