In this HackerRank Bigger is a Greater problem you have Given a word, create a new word by swapping some or all of its characters. This new word must be greater than the original word and also It must be the smallest word that meets the first condition.

HackerRank Bigger is Greater problem solution


Problem solution in Python programming.

for _ in range(int(input())):
    s = input()
    s = list(s[::-1])
    done = 0
    for i in range(1,len(s)):
        if s[i-1] > s[i]:
            for j in range(i):
                if s[j] > s[i]:
                    s[j],s[i] = s[i],s[j]
                    s = sorted(s[:i])[::-1] + s[i:]
                    print("".join(s[::-1]))
                    break
            break
    else:
        print("no answer")


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine();
        if(t>=1&&t<=100000){
            for(int i=0;i<t;i++){
                String str = sc.nextLine();
                System.out.println(process(str));
            }
        }
    }
    
    private static String process(String str){
        char[] chars = str.toCharArray();
        int i= chars.length-1;
        while(i>0){
            if(chars[i-1]>=chars[i]){
                i--;
            }else{
                int j=i;
                while(j<chars.length&&chars[j]>chars[i-1]){
                    j++;
                }
                char temp = chars[i-1];
                chars[i-1]=chars[j-1];
                chars[j-1]=temp;
                
                char[] newChar = new char[chars.length];
                for(int k=0;k<i;k++){
                    newChar[k]=chars[k];
                }
                int end = chars.length-1;
                for(int k=i;k<chars.length;k++){
                    newChar[k]=chars[end--];
                }
                return new String(newChar);
            }
        }
        return "no answer";
    }
    
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

 string s;

int main() {
    int tc;
    scanf("%d", &tc);
    while (tc--) {
        cin >> s;
        if (next_permutation(s.begin(), s.end())) printf("%s\n", s.c_str());
        else printf("no answer\n");
    }
    return 0;
}


Problem solution in C programming.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

void quick_sort (char *a, int n) {
    if (n < 2)
        return;
    char p = a[n / 2];
    char *l = a;
    char *r = a + n - 1;
    while (l <= r) {
        if (*l < p) {
            l++;
        }
        else if (*r > p) {
            r--;
        }
        else {
            char t = *l;
            *l = *r;
            *r = t;
            l++;
            r--;
        }
    }
    quick_sort(a, r - a + 1);
    quick_sort(l, a + n - l);
}

int main(int argc, char *argv[])
{
    int T;
    scanf("%u", &T);
    for (int t = 0; t < T; t++)
    {
        char s[101];
        scanf("%s", s);
        int sl = strlen(s);
        for (int i = sl - 2; i >= 0; i--)
        {
            for (int j = sl - 1; j > i; j--)
            {
                if (s[i] < s[j])
                {
                    char c = s[i];
                    s[i] = s[j];
                    s[j] = c;
                    quick_sort(s + i + 1, sl - i - 1);
                    goto found;
                }
            }
        }
        strcpy(s, "no answer");
found:
        printf("%s\n", s);
    }
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    
    
    var lines = input.split('\n');
    solutions = '';
    
    for (var i=1; i<lines.length; i++) {
        var w = lines[i];
        var numChars = w.length;
        if (numChars < 2) {
            solutions += 'no answer\n';
            continue;
        }
        
        // two characters or more:
        var wordBranchPos = -1;
        for (var n=w.length; n>1; n--) {
            var pair = w.substring(n-2,n);
            var pairAr = pair.split('');
            var pairArSorted = pairAr.sort();
            var pairSorted = pairArSorted.join('');
            if (pairSorted == pair) {
                var pairArReversed = pairArSorted.reverse();
                var pairReversed = pairArReversed.join('');
                if (pair == pairReversed) {
                    continue;
                }
                // The letter at n-2 is lower than n-1
                //var tail = w.substring(n-2);
                //w = w.substring(0,n-2) + pairReversed + w.substring(n);
                wordBranchPos = n;
                break;
            }
        }
        
        if (wordBranchPos != -1) {
            var n = wordBranchPos;
            var wStem = w.substring(0,n-2);
            var wBranch = w.substring(n-2);
            var wBranchAr = wBranch.split('');
            var wBranchArSorted = wBranchAr.sort();
            
            var breakChar = w.substring(n-2,n-1);
            var breakCharPos = wBranchArSorted.lastIndexOf(breakChar);
            var nextChar = wBranchArSorted[breakCharPos+1];
            wBranchArSorted.splice(breakCharPos+1,1);
            var newBranch = wBranchArSorted.join('');
            
            w = wStem + nextChar + newBranch;
            solutions += w + '\n';
        } else {
            solutions += 'no answer\n';
        }
        
    }
    
    console.log(solutions);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});