Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lessons;
- import java.util.ArrayList;
- public class combinations {
- // the initial array
- ArrayList<Integer> arr1=new ArrayList<Integer>();
- static ArrayList<String> allwords=new ArrayList<String>();
- //at the begining string is zero str is the string that would be added to the array
- String str="";
- String newstr;
- //temp string
- String temp="";
- //initial string from the array
- String instr="";
- String a="";
- String b="";
- String c="";
- int k=1;
- // the array that the devide would store
- //ArrayList<String> al=new ArrayList<String>();
- // the method to find the string from the array
- public String inString(ArrayList arr3){
- for(int i=0;i<arr3.size(); i++){
- a=arr3.get(i).toString();
- instr=instr+a;
- }
- return instr;
- }
- public String replace(String oldstr, String ch){
- //int index=oldstr.indexOf(ch);
- newstr=oldstr.replaceFirst(ch, "");
- return newstr;
- }
- public boolean recurse(ArrayList<String> arr2, int size){
- if(arr2.size()==1){
- str=str+arr2.get(0);
- if(allwords.size()==0){
- allwords.add(str);
- }else{
- for(int i=0; i<allwords.size();i++){
- if(allwords.get(i).equals(str)){
- break;
- }
- if(i==allwords.size()-1){
- allwords.add(str);
- }
- }}
- //allwords.add(str);
- str=str.replaceFirst(arr2.get(0), "");
- return true;
- }
- for(int i=0; i<arr2.size(); i++){
- if(i!=0){
- a=arr2.get(0);
- b=arr2.get(i);
- arr2.set(0, b);
- arr2.set(i, a);
- }
- if(k==arr2.size()){
- k=0;
- }
- ArrayList<String> al=new ArrayList<String>(arr2.subList(k, arr2.size()));
- str=str+arr2.get(0);
- recurse(al,size);
- str=str.replaceFirst(arr2.get(0), "");
- }
- return true;
- }
- public static void main(String[] args){
- combinations comb=new combinations();
- ArrayList<String> aaa=new ArrayList<String>();
- aaa.add("1");
- aaa.add("2");
- aaa.add("3");
- aaa.add("4");
- aaa.add("5");
- aaa.add("5");
- comb.inString(aaa);
- comb.recurse(aaa,0);
- for(int i=0; i<allwords.size();i++){
- int t=i+1;
- System.out.println(allwords.get(i)+" "+t);
- }
- /* combinations com=new combinations();
- String s="Hello world";
- String s1="l";
- System.out.println(com.replace(s, s1));*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement