[英]Permutations of a string using a stack in Java
I am having a problem figuring out why my code wont work. 我在弄清楚为什么我的代码无法正常工作时遇到了问题。 The code is supposed to push a string onto a stack.
该代码应该将字符串压入堆栈。 then it will put a "+" sign in front of the string.
那么它将在字符串前面放置一个“ +”号。 (example [+dog]. Next, it pops the stack. It moves the Characters on the right side of the "+" sign to the left side in the string, one by one(ex. [d+og] [o+dg] [g+do] and push these onto the stack. this repeats over and over until all the permutations of the string end with a plus sign(example [dog+][dgo+][god+][gdo+][odg+][ogd+] it then sees if the string on the stack has a "+" sign at the end, and if it does it prints the statement out. Here is my code:
(例如[+ dog]。接下来,它弹出堆栈。它将“ +”符号右侧的字符逐个移动到字符串的左侧(例如[d + og] [o + dg] [g + do]并将它们压入堆栈。这会反复进行,直到字符串的所有排列都以加号结束(例如[dog +] [dgo +] [god +] [gdo +] [odg +] [ogd + ]然后查看堆栈上的字符串是否在末尾带有“ +”号,如果是,它将打印出该语句,这是我的代码:
The problem i'm having is in my last else statement. 我遇到的问题是我的其他else语句。 I cant get the characters to move to the other side of the "+" sign.
我无法使字符移动到“ +”符号的另一侧。
import java.util.Scanner;
import java.util.Stack;
import java.util.*;
import java.lang.*;
public class string_Perm{
public static void main(String[] args) {// this takes user input for a string and runs it through the program
Scanner inputString = new Scanner(System.in);
String userInput;
System.out.print("Enter a String: ");
userInput = inputString.next();
// test print statement below
//System.out.println(userInput);
PermString(userInput);
}
public static void PermString(String userInput){
Stack permStack = new Stack();
permStack.push("+" + userInput);//this pushes the user input string onto the stack.
// test print statement below
//System.out.println(permStack);
while(!permStack.isEmpty()){
String currentItem = (permStack.pop().toString());// it then pops the top of the stack while the stack isn't empty.
if(currentItem.endsWith("+")){
currentItem.substring(0,currentItem.length()-1);// if the string ends with a +, you print the string without the + at the end
System.out.println(currentItem);
}
else{// this else statement is where i am having trouble its supposed to split the string at the plus sign and then move each char one by one to infront of the "+" sign, then push it onto the stack.
String[] charArray = currentItem.split("\\+");
System.out.println(charArray[charArray.length-1]);
for(int i = 0;i <= charArray.length; i++){
String getWord = charArray[charArray.length - 1];
String addWord = (charArray[i] + "+" + getWord.substring(0,i) + getWord.substring(i,0));
System.out.println(addWord);
permStack.push(addWord);
}
}
}
}
}
Any changes to this code or different methods are welcome. 欢迎对此代码或其他方法进行任何更改。 Thanks in advance.
提前致谢。
You have problems with your indexes. 您的索引有问题。 My suggestions:
我的建议:
charArray
(charArray holds the string not the characters!). charArray
第二个字符串的charArray
(charArray保存字符串而不是字符!)。 getWord.substring()
. getWord.substring()
。 Here is what I suggest for the code in the for loop: 这是我建议的for循环代码:
String[] charArray = currentItem.split("\\+");
for(int i = 0; i < charArray[1].length(); i++){
String getWord = charArray[charArray.length - 1];
StringBuilder sb = new StringBuilder(getWord);
String addWord = (getWord.charAt(i) + charArray[0] + "+" + sb.deleteCharAt(i).toString());
System.out.println(addWord);
permStack.push(addWord);
}
Try this and let me know if this is what you want. 试试这个,让我知道这是否是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.