String တခုရဲ့ permutation ကို ရွာေပးတဲ့ class

String တခုရဲ့ permutation ကို ရွာေပးတဲ့ class
=========
#permutation
String တခု၊ ဥပမာ "ABCD" ကို ထည့္ေပးလိုက္ရင္ သူ႔ရဲ့ permutation အတြဲေတြကို ရွာၿပီး Set<String> အျဖစ္ ျပန္ေပးပါတယ္။
=========
public class Permutation{
public static Set<String> find(String str) {
Set<String> perm = new HashSet<String>();
//Handling error scenarios
if (str == null) {
return null;
} else if (str.length() == 0) {
perm.add("");
return perm;
}
char initial = str.charAt(0); // first character
String rem = str.substring(1); // Full string without first character
Set<String> words = permutationFinder(rem);
for (String strNew : words) {
for (int i = 0;i<=strNew.length();i++){
perm.add(charInsert(strNew, initial, i));
}
}
return perm;
}
private static String charInsert(String str, char c, int j) {
String begin = str.substring(0, j);
String end = str.substring(j);
return begin + c + end;
}
}
============
အသံုးျပဳပံု
=======
Set<String> result=Permutation.find("ABCD");
ဒါဆိုရင္ result က
[DABC, CADB, BCAD, DBAC, BACD, ABCD, ABDC, DCBA, ADBC, ADCB, CBDA, CBAD, DACB, ACBD, CDBA, CDAB, DCAB, ACDB, DBCA, BDAC, CABD, BADC, BCDA, BDCA]
အျဖစ္ ရလာပါမယ္။
Set<String> result=Permutation.find("AAC");
ဒါဆိုရင္ result က
[AAC, ACA, CAA]
ျဖစ္ပါမယ္။
=========

Comments

Popular posts from this blog

သခ်ၤာ တြက္ခ်က္မႈ ဆိုင္ရာ နည္းလမ္းမ်ား

Software Development Library