1) Rank program in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class rank { | |
public static int factorial(int number){ | |
if (number == 1 || number==0) | |
return 1; | |
return number * factorial(number-1); | |
} | |
public static int getRank(String test){ | |
char[] chararray = test.toCharArray(); | |
Arrays.sort(chararray); | |
List<Character> lst = new LinkedList<>(); | |
for (char c :chararray){ | |
lst.add(c); | |
} | |
int sum = 0; | |
int length = chararray.length; | |
for(char c: test.toCharArray()){ | |
int index = lst.indexOf(c); | |
sum = sum + (index)* factorial(length-1); | |
length=length-1; | |
lst.remove(Character.valueOf(c)); | |
} | |
return sum; | |
} | |
public static void main(String args[]){ | |
String test = "BCA"; | |
int rank = getRank(test); | |
System.out.println(rank); | |
} | |
} |
No comments:
Post a Comment