@@ -0,0 +1,1971 @@
+package com.be1ive.hackerrank.strings;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Scanner;%0A%0A/**%0A * https://www.hackerrank.com/challenges/bigger-is-greater%0A *%0A * Problem Statement%0A *%0A * Given a word w, rearrange the letters of w to construct another word s in such a way that, s is lexicographically greater than w.%0A * In case of multiple possible answers, find the lexicographically smallest one.%0A *%0A * Input Format%0A * The first line of input contains t, number of test cases. Each of the next t lines contains w.%0A *%0A * Constraints%0A * 1 %E2%89%A4 t %E2%89%A4 105%0A * 1 %E2%89%A4 %7Cw%7C %E2%89%A4 100%0A * w will contain only lower case english letters and its' length will not exceed 100.%0A *%0A * Output Format%0A * For each testcase, output a string lexicographically bigger than w in a separate line.%0A * In case of multiple possible answers, print the lexicographically smallest one and if no answer exists, print no answer.%0A *%0A */%0Apublic class BiggerIsGreater %7B%0A%0A%0A    /**%0A     * Complexity: : O(N*log(N))%0A     **/%0A    public static void main(String%5B%5D args) %7B%0A        Scanner sc = new Scanner(System.in);%0A%0A        int T = sc.nextInt();%0A        for (int t = 0; t %3C T; t++) %7B%0A            String str = sc.next();%0A            char%5B%5D s = str.toCharArray();%0A            int length = s.length;%0A%0A%0A            int i = length - 1;%0A            while (i %3E 0 && s%5Bi%5D %3C= s%5Bi - 1%5D) %7B%0A               i--;%0A            %7D%0A%0A            if (i != 0) %7B%0A                int j = i;%0A                int k = i;%0A                while (j %3C length) %7B%0A                    if (s%5Bj%5D %3E s%5Bi - 1%5D && s%5Bj%5D %3C s%5Bk%5D) %7B%0A                        k = j;%0A                    %7D%0A                    j++;%0A                %7D%0A%0A                char tmp = s%5Bi - 1%5D;%0A                s%5Bi - 1%5D = s%5Bk%5D;%0A                s%5Bk%5D = tmp;%0A                Arrays.sort(s, i, length);%0A%0A                String bigger = new String(s);%0A                System.out.println(bigger);%0A            %7D else %7B%0A                System.out.println(%22no answer%22);%0A            %7D%0A        %7D%0A    %7D%0A%7D%0A

Bigger is greater