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++实验9 图及图的操作实验
    Html/CSS 示例演练 图书馆后台界面
    c++实验8 哈夫曼编码-译码器
    Html/CSS 初步介绍html和css部分重要标签
    c++实验7 二叉树
    JavaWeb -学生信息管理实践(JDBC+web+三层架构+DBUtil构造思路)
    java 实验6 图形用户界面设计试验(2)
    操作表与操作表数据行
    数据表操作练习
    数据库的介绍:
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7685031.html
Copyright © 2011-2022 走看看