Sunday 15 May 2011

permutation - Finding the number of ways a word can be permuted in java -


I am writing a program in java that a word can be sorted / rearranged.

For example, the term "happy" can be rearranged in 60 forms.

My process is to find the length of the word factorial, then dividing it by multiplying the factories of events, different characters in the word (Using recursive).

This is my code:

  import java.io.buffferedReader; Import java.io.IOException; Import java.io.InputStreamReader; Public Class Main {Public Static Ent Factile (Int Facts) {if (Fact == 0) returns 1; And if (fact == 0) returns 1; Return (fact * (factual (fact-1)); } Public static zero main throws (string []] IOException {string word; Full result; Integer cases, length forward; Int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y , Z; Buffer reader br = new buffed reader (new inputstreamer (system.)); Cases = integer PRIIENT (Breadline ()); (Intrateration = 1; iTration and lieutenant; = case; iTration ++) for {word = br.readLine (); LengthOfWord = word.length (); A = Longhofard - The word. Location ("A", "") Length (); B = Langhoford - the word. Location ("b", ""). Length (); C = lengthfold - word. Location ("c", ""). Length (); D = Longhound - word. Place ("D", "") Length (); E = Longhoford - the word. Location ("E", ""). Length (); F = Length ofWord - word.replace ("F", ""). Length (); G = length ofWord - word.replace ("G", ""). Length (); H = lengthfold - word. Location ("H", ""). Length (); I = Longoward- word Location ("I", "") Length (); J = lengthfold - word. Location ("J", "") Length (); K = lengthfords - words. Location ("of", "") Length (); L = Length OfWord - word.replace ("L", ""). Length (); M = lengthfords - words Location ("M", "") Length (); N = lengthfords - words. Place ("n", "") Length (); O = long word - word. Location ("O", "") Length (); P = Langhoford - the word. Location ("p", ""). Length (); Q = lengthOfWord - word.replace ("Q", ""). Length (); R = lengthfords - words. Place ("R", "") Length (); S = Length ofWord - word.replace ("S", ""). Length (); T = Longhofard - The word. Location ("T", "") Length (); U = Langhoford - the word. Location ("U", ""). Length (); V = lengthOfWord - word.replace ("V", ""). Length (); W = Length ofWord - word.replace ("W", ""). Length (); X = lengthOfWord - word.replace ("X", ""). Length (); Y = lengthfold - word. Location ("Y", ""). Length (); Z = Length ofWord - word.replace ("Z", ""). Length (); Factorial (F) * Factorial (H) * (Factorial (A) * Factorial (B) * Factorial (C) * Factorial (D) * Factorial (E) * Factorial (F) * Factorial (G) * Factorial (H) * Factorial I) * False (j) * factorial (k) * factorial (l) * factorial (m) * factorial (n) * factorial (o) * factorial (p) * factorial (q) * factorial (r) * factorial S) * factorial (u) * factorial (u) * factorial (v) * factorial (w) * factorial (x) * factorial (y) * factorial (z); System.out.printf ("Data set% d:% d \ n", repetition, result); }}}   

But I think it is not too long and efficient.

How can I make this program more efficient?

I also want to know about other ways to solve this program.

Please help thank you.

You can place letters in an array of length 26. Also, the number of string replacement character to check is very complex. Just loop through the letters and count them. Letter [word.charAt (j) - 'A'] Construction is such an implant that counts for A, at index 0, B is on index 1. Use the loop to multiply the focacities together. And change the word to upper case In the end, always declare variables as much as possible where they are actually used. (The last one is a normal general practice.)

By putting it together:

  throws public static zero principal (string array []) IOException {BufferedReader br = new BufferedReader ( New Impactstream Reader (System.in); Int case = integer. ParisInt (breadline ()); {Int letters = new int [26] for (int i = 0; i & lt; cases; i ++); String word = br.readLine (). ToUpperCase (); Int lengthOfWord = word.length (); For (int j = 0; j and lt; langoford; j ++) {letters [words Chatt (J) - 'A'] ++; } Int factorialProduct = 1; For (int j = 0; j and lt; letter long; j ++) {factual product * = factorial (letter [ja]); } Int Result = Factorial (Longhaired Ford) / Origin Product; System.out.printf ("Data set% d:% d \ n", repetition, result); }}    

No comments:

Post a Comment