Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use leetcode to realize the permutation and combination of repetitive strings in golang

Shulou Source: shulou.com Published: 2022-06-02 05:50:44 10月26日 Update

This article is about how to use leetcode to achieve the arrangement and combination of repetitive strings in golang. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

A permutation or combination of repeating strings. Write a method to calculate all permutations and combinations of a string.

Example 1:

Enter: s = "qqe"

Output: ["eqq", "qeq", "qqe"]

Example 2:

Enter: s = "ab"

Output: ["ab", "ba"]

Tip:

The characters are all English letters.

The length of the string is between [1,9].

Problem-solving ideas

1. This kind of problem is usually solved recursively

2. Deduplication logic is required due to the existence of repeating elements

3, traverse and take out each element in turn, and return the result splicing with the remaining elements.

4. For each element I in S, we need to determine whether it has appeared in S [: I]. If so, we can skip it.

Example:

Enter: s = "qqe"

Output: ["eqq", "qeq", "qqe"]

1, the first time we take out the first Q and then splice it with the result of "qe"

The second time we take the second Q, it is consistent with the result of the first calculation, so there is no need to calculate.

Code implementation

Func permutation (S string) [] string {var r [] string if len (S) = = 0 {r=append (r, ") return r} for iRO string if len (S)

Tags: Characters strings combinations elements results input output more knowledge examples first time articles practical consistent between code examples letters that is work will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Xiaomi macOS Apple