Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report