Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Creating all combinations

Ads

 
 

Share on Google+Share on Google+

Manjeet Sharma
Creating all combinations
4 Answer(s)      5 years and 3 months ago
Posted in : PHP

hello,

i want a script that can generate all the all words from given alphabets

for example

if i have three alphabets A,B,C

then a script that will generate the words as like below

a b

ab

bc

ca

ac

abc

cab

bac

acb

i mean all the possible combinations

so please help me in this

thanks

Ads
View Answers

April 9, 2012 at 11:17 AM


import java.util.*;

public class PermutationExample {
    public static void main(String args[]) throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter String: ");
        String chars = input.next();
        showPattern("", chars);
    }

    public static void showPattern(String st, String chars) {
        if (chars.length() <= 1)
            System.out.println(st + chars);
        else
            for (int i = 0; i < chars.length(); i++) {
                try {
                    String newString = chars.substring(0, i)
                            + chars.substring(i + 1);
                    showPattern(st + chars.charAt(i), newString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }
}


April 9, 2012 at 11:18 AM


function permute($str,$i,$n) {
   if ($i == $n)
       print "$str\n";
   else {
        for ($j = $i; $j < $n; $j++) {
          swap($str,$i,$j);
          permute($str, $i+1, $n);
          swap($str,$i,$j);
       }
   }
}

function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}   

$str = "hey";
permute($str,0,strlen($str));


April 9, 2012 at 11:19 AM


$string = "ABC";  
$strings = explode(' ', $string);

print_r(concat($strings, ""));

function concat(array $array, $base_string) {

    $results = array();
    $count = count($array);
    $b = 0;
    foreach ($array as $key => $elem){
        $new_string = $base_string . " " . $elem;
        $results[] = $new_string;
        $new_array = $array;

        unset($new_array[$key]);

        $results = array_merge($results, concat ($new_array, $new_string));

    }
    return $results;
}


April 9, 2012 at 7:52 PM


function permute($str,$i,$n) {
   if ($i == $n)
       print "$str\n";
   else {
        for ($j = $i; $j < $n; $j++) {
          swap($str,$i,$j);
          permute($str, $i+1, $n);
          swap($str,$i,$j);
       }
   }
}

function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}   

$str = "hey";
permute($str,0,strlen($str));

this helps me but there is problem

when i type string hey it generates only this result

(hey hye ehy eyh yeh yhe)

but i want in this way

y

e

h

ye

he

ey

eh

eyh

yeh

hey

i mean all possible worlds that i can make from the given alphabets

these generated words must contain

all single abphabets

all combibation two alphabets

and all combination from three alphabets

i want this script in php

thank


Ads









Related Tutorials/Questions & Answers:
Creating all combinations
Creating all combinations  hello, i want a script that can generate all the all words from given alphabets for example if i have three alphabets... abc cab bac acb i mean all the possible combinations so please help me
generating unique combinations of length of x in a string of length l
generating unique combinations of length of x in a string of length l   Generating unique combinations of length of x in a string of length l? Suppose a string abcd in read from the console and we have to produce all unique
Advertisements
how to do combinations in java - Java Beginners
how to do combinations in java  i have 2 arrayLists. i want do the combinations using those 2 lists. example: ArrayList a contains action,when,c1_p2,and arrayList b contains c1_p1,or, c2_p2,c3-p1,or,c4_p2 output is action
do the combinations in java, pls help me its urhent - Development process
do the combinations in java, pls help me its urhent  import java.util.*; interface Expression { public void interpret(ArrayList exp); } class OrExpression implements Expression { public void interpret(ArrayList exp
Creating an Applet
Creating an Applet  Hi, I have get a task regarding Creating an Applet using all Layout Manager, can anyone help me in this regard. I donā??t know..., first of all you should know about what is applet and then you should focus on how
Creating Exception
Creating Exception  class Myexception extends Exception{ private int detail; Myexecption(int a){ detail=a; } public String toString(){ return...){ System.out.println("Caught"+e); } } } hi to all please help me friends here i created my own
Creating Views
Creating Views Struts provides their own JSP tag library for creating view... contains all the basic html tag to create a page. Consider the simple page written... data by model and handle them appropriately. For creating a view you should
Creating a Frame
, calender, combobox checkbox and many more for creating GUI in Java based... with your applet. Swing libraries is used for creating desktop applications...; Creating a Frame     
Dynamically creating a form in gwt?
Dynamically creating a form in gwt?  how can i create a gwt form by retrieving fields dynamically from a map? i have all the fields set in the data... fields in a map and creating a form with them
creating java classes
creating java classes  Create a Java class that can be used to store... that shows all of the information that you have stored in the object Create a program that tests your class by creating and using at least two objects of the class
Creating Dynamic Tree
Creating Dynamic Tree   Creating Dynamic Tree: I have to build a tree structure with the following data lets say that with this data childId parentId 1 - 0 (lets say this is root) 2 - 1 3 - 1 4 - 3 5 - 3 6 - 5 7 - 6 all I need
Creating a memory leak with Java
Creating a memory leak with Java  Creating a memory leak with Java
Creating Menu - MobileApplications
Creating Menu  Hi all, I am developing an application for nokia mobiles and other java enabled phones. I have downloaded the NetBeans IDE and JDK 1.5 with Wireless toolkit and was able to create a simple application
creating a file in a directory
creating a file in a directory  hi i have tried all the file methods to create a file inside a directory but none of them r working some of them are new File ( ".", "C:/fileInRoot.txt" ) new File ( "myDir", "/otherDir" ) File
Creating an exe file
Creating an exe file  Hi All, I have developed java application using swing by Net Beans IDE. Now I need to convert my application into exe file to run a application without intervention of Net Beans IDE. How to convert
Creating URL in java
Creating URL in java  Hii to all I m trying to create a url using java. Address is http://www.merchant.com/pg/index.html When i m going to run...[] args) { try { // // Creating
Creating Database - SQL
Creating Database  Hi I am Guru I am having the confusion in creating the database.Actually Just I joined one small company. I am... ciao.co.uk. we are creating the database in mysql. 1> category->subcategory->
creating windows service - RMI
creating windows service  Hi, I have created a project using spring RMI and have some server.jar file which contians all the classes of server and server.exe file which is used to start the server. But I want to create a windows
to creating a registration form
to creating a registration form  how to create a registration form
creating windo chat
creating windo chat   hi ... please could you help me in creating chat aprograme in java ???? with comments beside codes please
creating a friendly url for web application
creating a friendly url for web application  Hi all, I don't know how to create a friendly url for a web application. Please help me to resolve this problem. Thanks, Suresh
Creating circle inside a circle
Creating circle inside a circle  hi can any one give code for following output by awt/swing creating circle inside a circle ,outside cirlce and intersect a circle
Creating PDF in JAVA
Creating PDF in JAVA  How create pdf in java ? Take value from database for particular PDF
creating inheritance of cars
creating inheritance of cars   hi ... please help me in creating inheritance between some types of cars like car that is parent and both of private car and public car are extends it then bus and truck extends public car
creating reports in java
creating reports in java  how to create reports automatically in java and send it via mail by scheduling tine for weekly,monthly basis
Creating a setup file
Creating a setup file  How to create a setup file in tomcat as done in visual studio... could plz tell me as early as possible
creating JD ialog
creating JD ialog   hi ... please help me in creatin a JDialog box that contains two buttons of minimization and exit
Creating Volume Bar Using Gauge Class
Creating Volume Bar Using Gauge Class       This example is all about creating a volume bar using Gauge class. We are creating the object of Gauge class that contains
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
creating trafficsignal using thread
creating trafficsignal using thread  please give me code how to create trafic signal using thread and give me it to my email [email protected] thanks