zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    指针,数组,字符串的区别(高质量程序设计指南C++/C语言第7章)
    bitset初始化问题
    书籍
    编译器的工作过程
    C++函数传递指向指针的指针的应用
    程序员面试金典--二叉树的下一个结点
    程序员面试金典--对称的二叉树
    程序员面试金典--按之字形顺序打印二叉树
    程序员面试金典--阶乘尾零
    程序员面试金典--矩阵元素查找
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7685031.html
Copyright © 2011-2022 走看看