Bigger is Greater HackerRank Solution in C, C++, Java, Python

Lexicographical order is often known as alphabetical order when dealing with strings. A string is greater than another string if it comes later in a lexicographically sorted list.

Given a word, create a new word by swapping some or all of its characters. This new word must meet two criteria:

  • It must be greater than the original word
  • It must be the smallest word that meets the first condition

For example, given the word w=abcd, the next largest word is .

Complete the function biggerIsGreater below to create and return the new string meeting the criteria. If it is not possible, return no answer.

Function Description

Complete the biggerIsGreater function in the editor below. It should return the smallest lexicographically higher string possible from the given string or no answer.

biggerIsGreater has the following parameter(s):

  • w: a string

Input Format

The first line of input contains T, the number of test cases.
Each of the next T lines contains w.

Constraints

  • 1<=T<=10^5
  • 1<=|w|<=100
  • w will contain only letters in the range ascii[a..z].

Output Format

For each test case, output the string meeting the criteria. If no answer exists, print no answer.

Sample Input 0

5
ab
bb
hefg
dhck
dkhc

Sample Output 0

ba
no answer
hegf
dhkc
hcdk

Explanation 0

  • Test case 1:
    ba is the only string which can be made by rearranging ab. It is greater.
  • Test case 2:
    It is not possible to rearrange bb and get a greater string.
  • Test case 3:
    hegf is the next string greater than hefg.
  • Test case 4:
    dhkc is the next string greater than dhck.
  • Test case 5:
    hcdk is the next string greater than dkhc.

Sample Input 1

6
lmno
dcba
dcbb
abdc
abcd
fedcbabcd

Sample Output 1

 

lmon
no answer
no answer
acbd
abdc
fedcbabdc

Bigger is Greater HackerRank Solution in C

#include <stdio.h>
char s[500];
int main()
{
    int t,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int l=strlen(s);
        int i=l-1;
        int f[300]={0};
        while(i>0&&s[i]<=s[i-1])
        {
            f[s[i]]++;
            i--;
        }
        if(i)
        {
            f[s[i]]++;
            f[s[i-1]]++;
            for(j=0;j<i-1;j++)
                printf("%c",s[j]);
            for(j=s[i-1]+1;j<='z';j++)
                if(f[j])
                    break;
        f[j]--;
            printf("%c",j);
            for(i='a';i<='z';i++)
                for(j=0;j<f[i];j++)
                    printf("%c",i);
            printf("\n");

        }
        else
            printf("no answer\n");
    }
    return 0;
}

 

Bigger is Greater HackerRank Solution in C++

#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;
}

 

Bigger is Greater HackerRank Solution in Java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Solution {
    static InputStream is;
    static PrintWriter out;
    static String INPUT = "";
    
    static void solve()
    {
        outer:
        for(int T = ni();T >= 1;T--){
            char[] s = ns().toCharArray();
            int n = s.length;
            int[] has = new int[26];
            for(int i = n-1;i >= 0;i--){
                has[s[i]-'a']++;
                for(int j = s[i]-'a'+1;j < 26;j++){
                    if(has[j] > 0){
                        s[i] = (char)('a'+j);
                        has[j]--;
                        int p = 0;
                        for(int k = i+1;k < n;k++){
                            while(p < 26 && has[p] == 0)p++;
                            s[k] = (char)('a'+p);
                            has[p]--;
                        }
                        out.println(new String(s));
                        continue outer;
                    }
                }
            }
            out.println("no answer");
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        long S = System.currentTimeMillis();
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        
        solve();
        out.flush();
        long G = System.currentTimeMillis();
        tr(G-S+"ms");
    }
    
    private static boolean eof()
    {
        if(lenbuf == -1)return true;
        int lptr = ptrbuf;
        while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
        
        try {
            is.mark(1000);
            while(true){
                int b = is.read();
                if(b == -1){
                    is.reset();
                    return true;
                }else if(!isSpaceChar(b)){
                    is.reset();
                    return false;
                }
            }
        } catch (IOException e) {
            return true;
        }
    }
    
    private static byte[] inbuf = new byte[1024];
    static int lenbuf = 0, ptrbuf = 0;
    
    private static int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }
    
    private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
    
    private static double nd() { return Double.parseDouble(ns()); }
    private static char nc() { return (char)skip(); }
    
    private static String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    
    private static char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }
    
    private static char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }
    
    private static int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }
    
    private static int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private static long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

 

Bigger is Greater HackerRank Solution in Python

def next(a):
    i = len(a) - 2
    while not (i < 0 or a[i] < a[i+1]):
        i -= 1
    if i < 0:
        return False
    j = len(a) - 1
    while not (a[j] > a[i]):
        j -= 1
    a[i], a[j] = a[j], a[i]
    a[i+1:] = reversed(a[i+1:])
    return True
for i in xrange(input()):
    w = list(raw_input().strip())
    if next(w):
        print ''.join(w)
    else:print 'no answer'

 

Bigger is Greater HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int t = int.Parse(Console.ReadLine());
        while(t-- > 0){
            char[] chr = Console.ReadLine().Trim().ToCharArray();
            int len = chr.Length;
            bool poss = false;
            int[] pos = new int[26];
            int idx = -1;
            int w = -1;
            bool[] small = new bool[26];
            small[(int)chr[len-1] - (int)'a'] = true;
            pos[(int)chr[len-1] - (int)'a'] = len - 1;
            for(int i = len - 2 ; i >= 0 ; i--){
                int acode = (int)chr[i] - (int)'a';
                for(int j = acode + 1; j < 26 ; j++){
                    
                    if(small[j])
                        {
                        poss = true;
                        idx = pos[j];
                        break;
                    }
                }
                if(poss){
                    w = i ;
                    break;
                }
                small[acode] = true;
                pos[acode] = i;
            }
            //Console.WriteLine(w + "-" + idx);
            if(poss){
                char c = chr[w];
                chr[w] = chr[idx];
                chr[idx] = c;
                char[] sortarr = new char[len - w - 1];
                int counter = 0 ;
                for(int i = w + 1 ; i < len ; i++){
                    sortarr[counter] = chr[i];
                    counter++;
                }
                Array.Sort(sortarr);
                for(int i = 0 ; i < sortarr.Length ; i++){
                    chr[w + 1] = sortarr[i];
                    w++;
                }
                Console.WriteLine(new string(chr));
            } else {
                Console.WriteLine("no answer");
            }
        }
    }
}

 

Attempt Bigger is Greater HackerRank Challenge

Link – https://www.hackerrank.com/challenges/bigger-is-greater/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/modified-kaprekar-numbers-hackerrank-solution/

Leave a Comment