java字符串字符排列组合_如何在Java中查找字符串的所有排列

java字符串字符排列组合

In this tutorial, we will learn how to find the permutation of a String in a Java Program. It’s a tricky question and asked mostly in Java interviews.

在本教程中,我们将学习如何在Java程序中查找字符串的排列。 这是一个棘手的问题,主要在Java访谈中提出。

Java中字符串置换的算法 (Algorithm for Permutation of a String in Java)

We will first take the first character from the String and permute with the remaining chars.

我们将首先从String中获取第一个字符,并用剩余的字符进行置换。

If String = “ABC”

如果String =“ ABC”

First char = A and remaining chars permutations are BC and CB.

第一个字符= A,剩余的字符排列为BC和CB。

Now we can insert first char in the available positions in the permutations.

现在,我们可以在排列的可用位置插入第一个字符。

BC -> ABC, BAC, BCA

卑诗省-> ABC,BAC,BCA

CB -> ACB, CAB, CBA

CB-> ACB,CAB,CBA

We can write a recursive function to return the permutations and then another function to insert the first characters to get the complete list of permutations.

我们可以编写一个递归函数以返回排列,然后编写另一个函数以插入第一个字符以获取排列的完整列表。

Java程序打印字符串的排列 (Java Program to Print Permutations of a String)

package com.journaldev.java.string;

import java.util.HashSet;
import java.util.Set;

/**
 * Java Program to find all permutations of a String
 * @author Pankaj
 *
 */
public class StringFindAllPermutations {
    public static Set<String> permutationFinder(String str) {
        Set<String> perm = new HashSet<String>();
        //Handling error scenarios
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            perm.add("");
            return perm;
        }
        char initial = str.charAt(0); // first character
        String rem = str.substring(1); // Full string without first character
        Set<String> words = permutationFinder(rem);
        for (String strNew : words) {
            for (int i = 0;i<=strNew.length();i++){
                perm.add(charInsert(strNew, initial, i));
            }
        }
        return perm;
    }

    public static String charInsert(String str, char c, int j) {
        String begin = str.substring(0, j);
        String end = str.substring(j);
        return begin + c + end;
    }

    public static void main(String[] args) {
        String s = "AAC";
        String s1 = "ABC";
        String s2 = "ABCD";
        System.out.println("\nPermutations for " + s + " are: \n" + permutationFinder(s));
        System.out.println("\nPermutations for " + s1 + " are: \n" + permutationFinder(s1));
        System.out.println("\nPermutations for " + s2 + " are: \n" + permutationFinder(s2));
    }
}

I have used Set to store the string permutations. So that duplicates are removed automatically.

我用Set来存储字符串排列。 这样可以自动删除重复项。

输出量 (Output)

Permutations for AAC are: 
[AAC, ACA, CAA]

Permutations for ABC are: 
[ACB, ABC, BCA, CBA, CAB, BAC]

Permutations for ABCD are: 
[DABC, CADB, BCAD, DBAC, BACD, ABCD, ABDC, DCBA, ADBC, ADCB, CBDA, CBAD, DACB, ACBD, CDBA, CDAB, DCAB, ACDB, DBCA, BDAC, CABD, BADC, BCDA, BDCA]

That’s all for finding all permutations of a String in Java.

这就是查找Java中String的所有排列的全部。

GitHub Repository. GitHub Repository下载示例程序代码。

翻译自: https://www.journaldev.com/526/permutation-of-string-in-java

java字符串字符排列组合

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值