SHOW:
|
|
- or go back to the newest paste.
| 1 | //Author - Saurav Kalsoor | |
| 2 | - | // Finding Kth Smallest - JAVA |
| 2 | + | // Finding Kth Smallest - KOTLIN |
| 3 | ||
| 4 | - | import java.util.Vector; |
| 4 | + | import java.util.* |
| 5 | - | import java.util.*; |
| 5 | + | import java.lang.* |
| 6 | ||
| 7 | - | public class Test {
|
| 7 | + | var sc: Scanner = Scanner(System.`in`) |
| 8 | - | |
| 8 | + | |
| 9 | - | static Scanner sc = new Scanner(System.in); |
| 9 | + | fun main() {
|
| 10 | - | |
| 10 | + | val n: Int = sc.nextInt() |
| 11 | - | public static void swap(char ch[],int i,int j) {
|
| 11 | + | val k: Int = sc.nextInt() |
| 12 | - | char temp = ch[i]; |
| 12 | + | val s: String = sc.next() |
| 13 | - | ch[i] = ch[j]; |
| 13 | + | println(findingKthSmallest(n, k, s)) |
| 14 | - | ch[j] = temp; |
| 14 | + | } |
| 15 | ||
| 16 | - | |
| 16 | + | fun swap(ch: CharArray, i: Int, j: Int) {
|
| 17 | - | public static void findingAllRearrangements(char ch[],int n,Vector<String> v,int itr){
|
| 17 | + | val temp = ch[i] |
| 18 | - | if(itr==n-1){
|
| 18 | + | ch[i] = ch[j] |
| 19 | - | String s = ""; |
| 19 | + | ch[j] = temp |
| 20 | - | for(char i : ch) {
|
| 20 | + | } |
| 21 | - | s+=i; |
| 21 | + | |
| 22 | - | } |
| 22 | + | fun findingAllRearrangements(ch: CharArray, n: Int, v: Vector<String>, itr: Int) {
|
| 23 | - | v.add(s); |
| 23 | + | if (itr == n - 1) {
|
| 24 | - | return; |
| 24 | + | var s: String = "" |
| 25 | for (i in ch) {
| |
| 26 | - | HashSet<Character> hs = new HashSet<Character>(); |
| 26 | + | s += i |
| 27 | - | for(int i=itr;i<n;i++) {
|
| 27 | + | |
| 28 | - | if(!hs.contains(ch[i])) {
|
| 28 | + | v.add(s) |
| 29 | - | hs.add(ch[i]); |
| 29 | + | return |
| 30 | - | swap(ch,itr,i); |
| 30 | + | |
| 31 | - | findingAllRearrangements(ch,n,v,itr+1); |
| 31 | + | val hs: HashSet<Char> = HashSet<Char>() |
| 32 | - | swap(ch,itr,i); |
| 32 | + | for (i in itr until n) {
|
| 33 | - | } |
| 33 | + | if (!hs.contains(ch[i])) {
|
| 34 | hs.add(ch[i]) | |
| 35 | swap(ch, itr, i) | |
| 36 | - | |
| 36 | + | findingAllRearrangements(ch, n, v, itr + 1) |
| 37 | - | public static String findingKthSmallest(int n, int k, String s){
|
| 37 | + | swap(ch, itr, i) |
| 38 | - | char ch[] = s.toCharArray(); |
| 38 | + | |
| 39 | - | Vector<String> v = new Vector<String>(); |
| 39 | + | |
| 40 | - | findingAllRearrangements(ch,n,v,0); |
| 40 | + | } |
| 41 | - | Collections.sort(v); |
| 41 | + | |
| 42 | - | if(k<=v.size()) {
|
| 42 | + | fun findingKthSmallest(n: Int, k: Int, s: String): String {
|
| 43 | - | return v.get(k-1); |
| 43 | + | val ch: CharArray = s.toCharArray() |
| 44 | val v: Vector<String> = Vector<String>() | |
| 45 | - | return "-1"; |
| 45 | + | findingAllRearrangements(ch, n, v, 0) |
| 46 | Collections.sort(v) | |
| 47 | - | |
| 47 | + | return if (k <= v.size) {
|
| 48 | - | public static void main(String[] args) {
|
| 48 | + | v.get(k - 1) |
| 49 | - | int n = sc.nextInt(); |
| 49 | + | } else "-1" |
| 50 | - | int k = sc.nextInt(); |
| 50 | + | } |
| 51 | - | String s = sc.next(); |
| 51 | + |