Finding all possible combinations of a String using recursion - Full Java Code
Finding all possible combinations of a String using recursion - Full Java Code
This article is part of a tutorial.
Tutorial Index page - Java Data Modelling
/** * $Id: StringCombinations.java 100 2010-03-13 23:12:20Z oneyour $ * * This is an accompanying program for the article * <a href="http://www.1your.com/drupal/FullCodeFindingAllStringCombinationsUsingRecursion " title="http://www.1your.com/drupal/FullCodeFindingAllStringCombinationsUsingRecursion ">http://www.1your.com/drupal/FullCodeFindingAllStringCombinationsUsingRec...</a> * * Copyright (c) 2009 - 2010 <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a>. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ import java.io.IOException; import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * A simple program that generates all possible combinations for a given * String using recursion. * */ public class StringCombinations { // A set to hold the generated combination results. private Set combinations = new HashSet(); /** * The constructor. * * Simply makes a call to the generate method * to generate all the possible combinations of the * String "wxyz" */ public StringCombinations(String sInputString) { generate(sInputString); System.out.println("*** Generated " + combinations .size() + " combinations ***"); System.out.println(combinations); System.out.println("Visit <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> for more Java articles"); System.out.println("Whats even more great is you can request for articles and" + "see articles created just for your request!"); } /** * The recursive method does the work of generating all the possible * combinations for a given String. */ public void generate(String word) { // Add this word to our combination results set // System.out.println(word); combinations.add(word); // If the word has only one character we break the // recursion if (word.length() == 1) { combinations.add(word); return; } // Go through every position of the word for (int i = 0; i < word.length(); i++) { // Remove the character at the current position // all call this method with that String (Recursion!) generate(word.substring(0,i) + word.substring(i+1)); } } /** * Request and read input from the command line (System.in) * * @param inputMessage * The message to be displayed as a request to the user * * @return * The input from the command line * * @throws IOException * Any problems while reading the input */ public static String readCommandLineInput(String inputMessage) { System.out.println(inputMessage); Scanner scanner = new Scanner(System.in); String inputLine = scanner.nextLine(); return inputLine; } /** * Entry point to this program. * Instantiates the StringCombinations program. * */ public static void main(String args[]) { // Request and read user input String sInstruction = "Enter a String: \n"; String sInputString = readCommandLineInput(sInstruction); new StringCombinations(sInputString); } }// End of StringCombinations
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
