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

    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:

     1 class Solution {
     2     public String splitLoopedString(String[] strs) {
     3         for(int i = 0; i<strs.length; i++){
     4             String reverse = new StringBuilder(strs[i]).reverse().toString();
     5             if(strs[i].compareTo(reverse) < 0){
     6                 strs[i] = reverse;
     7             }
     8         }
     9         
    10         int len = strs.length;
    11         String res = "";
    12         for(int i = 0; i<len; i++){
    13             String reverse = new StringBuilder(strs[i]).reverse().toString();
    14             for(String s : new String[]{strs[i], reverse}){
    15                 for(int k = 0; k<s.length(); k++){
    16                     StringBuilder sb = new StringBuilder(s.substring(k));
    17                     
    18                     for(int j = i+1; j<len; j++){
    19                         sb.append(strs[j]);
    20                     }
    21                     for(int j = 0; j<i; j++){
    22                         sb.append(strs[j]);
    23                     }
    24                     
    25                     sb.append(s.substring(0, k));
    26                     if(sb.toString().compareTo(res) > 0){
    27                         res = sb.toString();
    28                     }
    29                 }
    30             }
    31         }
    32         return res;
    33     }
    34 }
  • 相关阅读:
    seata 1.3.0 seata Global lock wait timeout
    Tika解析word文件
    我的第一款微信小程序:iteye附件下载器,希望大家好好爱惜
    读书《尸检报告》 [英]卡拉·瓦伦丁 / 中信出版集团2019-08
    读书《另一种选择》 [美] 谢丽尔·桑德伯格 / 中信出版集团2017-08
    读书《不朽的失眠》 张晓风 / 四川人民出版社2018-09
    Uniapp 修改内置组件样式无效解决方法
    Android studio中.9图片的含义及制作教程
    Diff算法
    js new一个对象的过程,实现一个简单的new方法
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7685031.html
Copyright © 2020-2023  润新知