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 scenariosif (str == null) {return null;} else if (str.length() == 0) {perm.add("");return perm;}char initial = str.charAt(0); // first characterString rem = str.substring(1); // Full string without first characterSet<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字符串字符排列组合

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

  1. c语言将字符串按空格分割_如何在c++中实现字符串分割函数split详解

    前言 在学习c++中string相关基本用法的时候,发现了sstream的istringstream[1]可以将字符串类似于控制台的方式进行输入,而实质上这个行为等同于利用空格将一个字符串进行了分割, ...

  2. python输出字符串中的大写字母_如何在python中查找字符串中的大写字母

    我想在一个字符串的任何位置找到大写字母,我想如果一个字符串在一个字符串的任何位置由大写字母组成,那么应该打印字符串"1",如果字符串在任何位置不包含任何大写字母,那么应该打印字符串 ...

  3. 表格在整个html居中显示,html 表格字符居中显示_如何在HTML中居中显示表格?

    html 表格字符居中显示_如何在HTML中居中显示表格? html 表格字符居中显示_如何在HTML中居中显示表格? html 表格字符居中显示 HTML table provides the ab ...

  4. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  5. php根据字符串分割字符串_如何在PHP中按字符串分割字符串?

    php根据字符串分割字符串 How to split a string by string in PHP? For example, 如何在PHP中按字符串分割 字符串 ? 例如, "a s ...

  6. golang 格式化字符串_如何在Go中格式化字符串

    golang 格式化字符串 As strings are often made up of written text, there are many instances when we may wan ...

  7. java char字符转编码_一、java基础-数据类型_数据类型转化_字符编码_转义字符

    1.Java  支持的8种基本数据类型: java的四种整数数据类型:byte 1    short 2     int4     long8 byte     8位带符号整数 -128到127之间的 ...

  8. c++ 排列组合_省考行测数量关系的老大难,排列组合的基本类型题及秒杀技巧...

    作者:步知公考上岸学员 Jud°米娅 不知不觉省考剩下50多天了,这50多天要想行测更上一层楼,除了保证资料分析等模块的准确率(70%-80%),数量关系也不可以丢太多的分数,而数量关系自认为最难的题 ...

  9. mysql中转换成字符串_如何在R中转换字符串的大小写?

    mysql中转换成字符串 Hello, folks. In this tutorial we are going to convert the case of the string in R. The ...

最新文章

  1. BUZZER Driver
  2. win7系统怎么获取system权限?
  3. 瑞士桁架机器人_机器人库晚报:人工智能可以在实验室中预测人的血糖水平
  4. 另一种办法直接在宿主机上的文件夹内查看Docker镜像运行的日志文件
  5. excel有必要用python_为什么Python比VBA更适合自动化处理Excel数据?
  6. 我想成为计算机专业第一,我对计算机专业学生的忠告。
  7. 这些智能合约漏洞,可能会影响你的账户安全!
  8. 【BZOJ1911】【codevs1318】特别行动队,斜率优化DP
  9. 27 Python - 数值 日期与时间
  10. 在Windows平台使用IIS部署Flask网站
  11. 上下文路径request.getContextPath();与${pageContext.request.contextPath}
  12. 简明人体结构(一):人体结构学习方式的整体引导
  13. openstack搭建教程
  14. (三) u-boot 启动分析_第一阶段
  15. 唠一唠 消息可靠性保障消息幂等性处理 (RabbitMQ实际应用问题)
  16. 宝塔服务器源代码修改记录,宝塔BT面板修改相关记录,所有插件免费用
  17. Python实现斗地主
  18. Redis的可视化工具查询内容为 --> “\xac\xed\x00\x05t\x00”乱码
  19. UDS协议发展历史(UDS是什么?)
  20. 贵州大学计算机专业男女比例,全国高校男女比例排行榜出炉! 妈, 我大学估计真找不到对象了...

热门文章

  1. 做一款仿映客的直播App?看我就够了
  2. Asp.net MVC 搭建属于自己的框架(一)
  3. HDU 1010 深搜+奇偶剪枝
  4. 高速串行总线走线难点在哪?重要线信号的处理经验分享
  5. Python socket – network programming tutorial
  6. 微信小程序手机号快速填写及会员卡开卡组件开放
  7. springmvc跳转到自定义404页面的三种方法
  8. CListCtrl,SetItemState 高亮(显示蓝色)
  9. C# Readonly和Const的区别
  10. java port_Java NonRegisteringDriver.port方法代码示例