Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Split Concatenated Strings

Posted on August 5, 2019July 26, 2020 by braindenny

Split Concatenated Strings



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #string, #greedy

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.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/split-concatenated-strings
// Basic Ideas:
//
//  Notice:  For non-splited ones, we can always 
//    choose the bigger one original or reversed version.
//
// Complexity: Time O(n^2), Space O(n)
func reverse(bytes []byte) {
    left, right := 0, len(bytes)-1
    for left<right {
        bytes[left], bytes[right] = bytes[right], bytes[left]
        left, right = left+1, right-1
    }
}

func splitLoopedString(strs []string) string {
    for i, s := range strs {
        bytes := []byte(s)
        reverse(bytes)
        s2 := string(bytes)
        if s2 > s {
            strs[i] = s2
        }
    }
    res := ""
    for i, s := range strs {
        reverseBytes := []byte(s)
        reverse(reverseBytes)
        for _, bytes := range [][]byte {[]byte(s), reverseBytes} {
            for k, _ := range bytes {
                l := []byte{}
                l = append(l, bytes[k:]...)
                for j:=i+1; j<len(strs); j++ {
                    l = append(l, []byte(strs[j])...)
                }
                for j:=0; j<i; j++ {
                    l = append(l, []byte(strs[j])...)
                }
                l = append(l, bytes[0:k]...)
                s2 := string(l)
                if s2 > res {
                    res = s2
                }
            }
        }
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #greedy, #string

Post navigation

LeetCode: Maximum Profit in Job Scheduling
LeetCode: Remove Comments

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.