Full Java Code: Finding all possible combinations of a String using recursion

/**
* Copyright 2008 1your.com
*
*/


package com.oneyour.stringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String.;

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

/**
* @author deepak
*
* A simple program that generates all possible combinations for a given
* String using recursion.
*
*/

public class CombinationsOfStringRecurssion
{

    // 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 CombinationsOfStringRecurssion()
    {

        generate("ABCD");
        System.out.println("*** Generated " + combinations .size() + " combinations ***");
        System.out.println(combinations);
        System.out.println("Visit www.1your.com 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));

        }

    }

    /**
    * Entry point to this program.
    * Instantiates the CombinationsOfStringRecurssion program.
    *
    */

    public static void main(String args[])
    {

        new CombinationsOfStringRecurssion();

    }

}// End of CombinationsOfStringRecurssion

 

 

Comments

Optimization

When you break the recursion, you don't need to add the current word to the set of combinations since you've done that already two lines above. Of course, since combinations is a Set you can add the same element multiple times and it will only appear in the set once, but why do extra work when you don't have to?

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Full Java Code: Finding all possible combinations of a String using recursion | Our website now yours! - Currenlty Java focussed.

Full Java Code: Finding all possible combinations of a String using recursion

/**
* Copyright 2008 1your.com
*
*/


package com.oneyour.stringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String.;

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

/**
* @author deepak
*
* A simple program that generates all possible combinations for a given
* String using recursion.
*
*/

public class CombinationsOfStringRecurssion
{

    // 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 CombinationsOfStringRecurssion()
    {

        generate("ABCD");
        System.out.println("*** Generated " + combinations .size() + " combinations ***");
        System.out.println(combinations);
        System.out.println("Visit www.1your.com 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));

        }

    }

    /**
    * Entry point to this program.
    * Instantiates the CombinationsOfStringRecurssion program.
    *
    */

    public static void main(String args[])
    {

        new CombinationsOfStringRecurssion();

    }

}// End of CombinationsOfStringRecurssion

 

 

Comments

Optimization

When you break the recursion, you don't need to add the current word to the set of combinations since you've done that already two lines above. Of course, since combinations is a Set you can add the same element multiple times and it will only appear in the set once, but why do extra work when you don't have to?

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options