AriyankiNet        
 


CodeworkingJavaArticle ≫ Algoritm case 1

Algoritm case 1

Tue 15 Sep 2015, 19:42


Im curious with this Algoritm case: You have 2 words separated by comma. You need to find how many combinations of words from the first word. The combination must be unique and contain the second word.

 

Input 1:

aabc,ba

Output:

combination: 6

(abac, acba, baac, baca, caba, cbaa)

 

Input 2: 

ferdiferdi,ferdi

Output: 

combination: 719

 

And this is my code and i don’t know this is the simple code or not:

 

import java.util.*;
import java.io.*;

class Main{
	static ArrayList al=new ArrayList();
	static int count=0;

	public static void main(String []argh)
	{
		String str="aabc,ba";
		String[] parts = str.split(",");
		String word=parts[0];
		for(int i=0;i<word.length();i++){
			String out="";
			out=word.substring(i)+word.substring(0,i);
			getComb(out.substring(1),out.substring(0,1),2,parts[1]);
		}
		System.out.println(count);
		
	}

	static void getComb(String word,String currChar,int lvl,String skipW){
		for(int i=0;i<word.length();i++){
			String out="";
			String out2="";
			out=word.substring(i)+word.substring(0,i);
			out2=currChar+out;
			int lvl2=lvl+1;
			if(lvl2<out2.length()){
				getComb(out.substring(1),out2.substring(0,lvl),lvl2,skipW);
			}else{

				if(out2.indexOf(skipW)>=0){
					if(!al.contains(out2)){
						al.add(out2);
						System.out.println(out2);
						count++;
					}
				}
			}
		}

	}
}

0 3279 views