w3resource

Java: Find lexicographic rank of a given string

Java String: Exercise-50 with Solution

Write a Java program to find the lexicographic rank of a given string.

Visual Presentation:

Java String Exercises: Find lexicographic rank of a given string

Sample Solution:

Java Code:

// Importing necessary Java utilities.
import java.util.*;

// Define a class named Main.
class Main {
  
  // Method to calculate factorial of a number recursively.
  public static int makefactorial(int n) {
    // If n is less than or equal to 2, return n. Otherwise, calculate n * factorial of (n - 1).
    return (n <= 2) ? n : n * makefactorial(n - 1);
  }

  // Method to calculate the lexicographic rank of a given string.
  public static int calcuLexicoRank(String str, int n) {
    // Counter for the rank initialized to 1.
    int ctrOfRank = 1;
    
    // Loop through each character in the string.
    for (int i = 0; i < n; i++) {
      int ctr = 0;
      
      // Compare the character at index i with subsequent characters.
      for (int j = i + 1; j <= n; j++) {
        // If the character at index i is greater than the character at index j, increment the counter.
        if (str.charAt(i) > str.charAt(j))
          ctr++;
      }
      
      // Calculate the lexicographic rank using the formula.
      ctrOfRank += ctr * makefactorial(n - i);
    }
    return ctrOfRank; // Return the calculated rank.
  }
  
  // Main method to execute the program.
  public static void main(String[] args) {
    String str = "BDCA";
    System.out.println("The Given String is: " + str);
    int n = str.length(); // Get the length of the string.
    // Display the lexicographic rank of the given string.
    System.out.print("The Lexicographic rank of the given string is: " + calcuLexicoRank(str, n - 1));
  }
}

Sample Output:

The Given String is: BDCA
The Lexicographic rank of the given string is: 12

N.B.: Total possible permutations of BDCA are(lexicographic order) :
ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC BDCA
1   2   3   4   5   6   7   8   9   10   11   12
The BDCA appear in 12 position of permutation (lexicographic order).

Flowchart:

Flowchart: Java String Exercises - Find lexicographic rank of a given string

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to find first non-repeating character from a stream of characters.
Next: Write a Java program to count and print all the duplicates in the input string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.