按字母顺序打印给定字符串中所有子字符串的组合

编程入门 行业动态 更新时间:2024-10-08 08:25:23
本文介绍了按字母顺序打印给定字符串中所有子字符串的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

已问过此问题并回答很多次.但是,我特别要求按照输出中显示的每个字母的特定顺序打印子字符串.

This question has been asked and answered plenty of times before. However, I'm specifically asking for the substrings to be printed in the specific order by each letter as shown in the output.

import java.util.*; public static void main(String[] args) { breakWordIntoPieces("ABIGWORD"); } public static void breakWordIntoPieces(String str) { if (str.length() > 1) // I'm skipping all substrings of less than 2 letters { List<String> fragments = breakWordIntoPiecesFromRightToLeft(str); for (String fragment : fragments) // I'm using the variable name "fragments" as // another word for "substring". { System.out.print("\n" + fragment); } } System.out.print("\n"); str = removeFirstChar(str, 1); List<String> fragments2 = breakWordIntoPiecesFromRightToLeft(str); for (String fragment : fragments2) { System.out.print("\n" + fragment); } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments3 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments3) { System.out.print("\n" + fragment); } } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments4 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments4) { System.out.print("\n" + fragment); } } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments5 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments5) { System.out.print("\n" + fragment); } } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments6 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments6) { System.out.print("\n" + fragment); } } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments7 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments7) { System.out.print("\n" + fragment); } } if (str.length() > 1) { str = removeFirstChar(str, 1); List<String> fragments8 = breakWordIntoPiecesFromRightToLeft(str); System.out.print("\n"); for (String fragment : fragments8) { System.out.print("\n" + fragment); } } } public static List<String> breakWordIntoPiecesFromRightToLeft(String word) { int sizeOfWord = word.length(); List<String> fragments = new ArrayList<>(); for (int i = 0; i < word.length() - 1; i++) { String aFragment = removeLastChar(word, i); fragments.add(aFragment); } return fragments; } private static String removeLastChar(String str, Integer i) { return str.substring(0, str.length() - i); //((Line 200)) remove last i letters. } public static String removeFirstChar(String s, Integer i) { return s.substring(i); // remove first i letters }

下面的输出是正确的.它以所需顺序打印所有可能的子字符串.这是所需的顺序,但是无法对代码进行硬编码.

The output below is correct. It prints all possible substrings in the desired order. This is the desired order, but the code can't be hard-coded.

输出:

ABIGWORD //First it prints the whole word. ABIGWOR //Then the rest below are all possible sub strings lined up in order by letter ABIGWO ABIGW ABIG ABI AB BIGWORD BIGWOR BIGWO BIGW BIG BI IGWORD IGWOR IGWO IGW IG GWORD GWOR GWO GW WORD WOR WO ORD OR RD

尽管这在技术上是可行的,但它显然是经过硬编码的,因此仅适用于8个字母或以下的单词.以下代码是利用上述所有其他方法的breakWordIntoPieces()的重构版本.

While this technically works, it's clearly hard coded and therefore only works for words up to around 8 letters or less. The following code below is a refactored version of breakWordIntoPieces() utilizing all other methods above.

public static void breakWordIntoPiecesRefactored(String str) { int subtractCharactersBy = 0; int lengthOfWord = str.length(); while (lengthOfWord > 1) //this if can be checked as the method is called { str = removeFirstChar(str, subtractCharactersBy); // ((Line 259) lengthOfWord -= 1; subtractCharactersBy +=1; List<String> fragments = breakWordIntoPiecesFromRightToLeft(str); for (String fragment : fragments) { System.out.print("\n" + fragment); } } }

输出:

ABIGWORD ABIGWOR ABIGWO ABIGW ABIG ABI AB BIGWORD BIGWOR BIGWO BIGW BIG BI GWORD //Why is it skipping the letter I straight to G? GWOR GWO GW RD Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at java.lang.String.substring(String.java:1931) at company.Controllers.SpellCheck.removeFirstChar(SpellCheck.java:200) at company.Controllers.SpellCheck.breakWordIntoPiecesRefactored(SpellCheck.java:259) at company.Main.main(Main.java:20) Process finished with exit code 1

我认为错误代码中的-2有重要意义.我认为这与向字符串减去比字符串本身长度更多的字母有关吗?

I think there's something important with that -2 in the error code. I think it has to do with subtracting more letters to a string than the length of the string itself?

推荐答案

您可以为此使用两个for循环,一个循环嵌套在另一个循环中.外循环从左移,内循环从右移.

You can use two for loops for this, one nested inside the other. The outer loop moves in from the left, the inner loop moves in from the right.

static void printPieces(String str, int min) { for(int i=0; i<=str.length()-min; i++) { for(int j=str.length(); j>=i+min; j--) { System.out.println(str.substring(i, j)); } System.out.println(); } }

对于printPieces("ABIGWORD", 2),我们得到:

ABIGWORD ABIGWOR ABIGWO ABIGW ABIG ABI AB BIGWORD BIGWOR BIGWO BIGW BIG BI IGWORD IGWOR IGWO IGW IG GWORD GWOR GWO GW WORD WOR WO ORD OR RD

更多推荐

按字母顺序打印给定字符串中所有子字符串的组合

本文发布于:2023-11-30 07:31:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   组合   字母   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!