Sort by

recency

|

979 Discussions

|

  • + 0 comments

    Java solution

     public static String biggerIsGreater(String w) {
    // Write your code here
    
      String[] strSplit = w.split("");
    
      for(int i = w.length() -1 ; i >= 0; i--){
          for(int j = w.length() -1 ; j > i; j--){
              if(strSplit[i].charAt(0) < strSplit[j].charAt(0)){
    
                  StringBuilder stringBuilder = new StringBuilder(w.substring(j+1));
                  StringBuilder stringBuilder2 = new StringBuilder(w.substring(i + 1,j));
    
                  return w.substring(0, i) + 
                  w.charAt(j) + 
                  stringBuilder.reverse().toString() + 
                  w.charAt(i) +
                  stringBuilder2.reverse().toString();
              }
          }
      }
    
      return "no answer";
    
    }
    
  • + 0 comments

    golang:

    func biggerIsGreater(w string) string {
        str := strings.Split(w, "")
    
        s_len := len(str)
    
        for i := 1; i < len(str); i++ {
    
            curr_index := s_len - i - 1
            curr := str[curr_index]
    
            rest := str[curr_index+1:]
    
            sort.Slice(rest, func(i, j int) bool {
                return rest[i] < rest[j]
            })
    
            for j, v := range rest {
                if v > curr {
                    initial := strings.Join(str[:curr_index], "")
                    rest = append(rest[:j], rest[j:]...)
    
                    rest[j] = curr
    
                    sort.Slice(rest, func(i, j int) bool {
                        return rest[i] < rest[j]
                    })
    
                    return initial + v + strings.Join(rest, "")
                }
            }
    
        }
    
        return "no answer" 
    }
    
  • + 1 comment

    C++ solution

    #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;
    }
    
  • + 0 comments
    
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'biggerIsGreater' function below.
    #
    # The function is expected to return a STRING.
    # The function accepts STRING w as parameter.
    #
    
    def biggerIsGreater(w):
        if "".join(sorted(w, reverse=True)) == w:
            return "no answer"
        length = len(w)
        if length == 2:
            return w[::-1]
        base_index = length -1
        while base_index > 0:
            if w[base_index] <= w[base_index-1]:
                base_index -= 1
            else:
                base_index -= 1
                break
        if base_index + 1 == length - 1:
            return w[:-2] + w[-1] + w[-2]
        result = w[:base_index]
        remainder = sorted(w[base_index:])
        base_num = w[base_index]
        highest = list(filter(lambda x: x>base_num, remainder))[0]
        remainder.remove(highest)
        result += highest + "".join(remainder)
        return result
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        T = int(input().strip())
    
        for T_itr in range(T):
            w = input()
    
            result = biggerIsGreater(w)
    
            fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.lang.StringBuilder;
    
    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 input = new Scanner(System.in);
            int T = input.nextInt();
            input.nextLine();
            
            for(int i = 0; i < T; i++)
            {    
                String word = input.nextLine();
                if(word.length()==1) {System.out.println("no answer");continue;}
                
                
                int maxLexoC1 = 0; //The max lexocographical according to condition 1
                int maxLexoC2 = 0; //The max lexocographical according to condition 2
                
                
                
                //Find the largest index char that is weakly increasing such as g in hefg 
                for(int j = 1; j < word.length(); j++)
                {
                    boolean condition1 = word.charAt(j) > word.charAt(j-1);
                        
                    if(condition1)
                    {
                        maxLexoC1 = (j > maxLexoC1) ? j : maxLexoC1;
                    }
                }
                
                //if our only increasing is at point 0 then we are in the last permuation of our string
                if(maxLexoC1==0) {System.out.println("no answer");continue;}
                
                //maxLexoC2
                //Determine the right most char greater than the pivot in index and in lexo
                for(int j = maxLexoC1; j < word.length(); j++)
                {
                    boolean condition2 = word.charAt(j) > word.charAt(maxLexoC1-1);
                        
                    if(condition2)
                    {
                        maxLexoC2 = j;
                    }
                }
                
                StringBuilder wordSB = new StringBuilder(word);
                
                //Swap the pivot with maxLexoC2
                char tmp = wordSB.charAt(maxLexoC1-1);
                wordSB.setCharAt(maxLexoC1-1, wordSB.charAt(maxLexoC2));
                wordSB.setCharAt(maxLexoC2, tmp);
                            
                
                //Reverse starting at the element to the right of the pivot
                int left = maxLexoC1;
                int right = wordSB.length()-1;
                while(left < right)
                {
                    //swap left with right
                    tmp = wordSB.charAt(left);
                    wordSB.setCharAt(left, wordSB.charAt(right));
                    wordSB.setCharAt(right, tmp);
                    left++;
                    right--;
                }
                
                
                System.out.println(wordSB);
            }
        }
    }