Sort by

recency

|

981 Discussions

|

  • + 1 comment
    def next_permutation(n):
        digits = list(str(n))  
        i = len(digits) - 2
        while i >= 0 and digits[i] >= digits[i + 1]:
            i -= 1
        j = len(digits) - 1
        while digits[j] <= digits[i]:
            j -= 1
        digits[i], digits[j] = digits[j], digits[i]
        digits = digits[:i+1] + digits[i+1:][::-1]
        return int(''.join(digits))
    def biggerIsGreater(s):
      s_new=''
      p=[]
      q=[]
      w_new=sorted(s)
      m_new=sorted(s,reverse=1)
      if ''.join(m_new)==s:
        return 'no answer'
      for i in enumerate(w_new):
        p.append(i[0]+1)
        q.append(i[1])
      for i in s:
        s_new+=str(p[q.index(i)])
        print(i,s_new,p[q.index(i)])
      a=''
      for i in str(next_permutation(int(s_new))):
        a+=q[int(i)-1]
      return a
    
  • + 0 comments

    My general algorithm for solving this is as follows: Let's use "dkhc" as the driving example.

    1. Start looking from the right, and find the first pair of letters that is out of order. Here, 'd' and 'k' are out of order.
    2. Find the smallest letter to the right of the out-of-order letter that is greater than it. Here, 'h' is the smallest letter that is greater than 'd'.
    3. Swap those two letters. This gives us "hkdc".
    4. Finally, sort all the letters to the right of the original out-of-order index. Here, that means sorting "kdc" into "cdk". Altogether then, "hcdk" is the final answer.
  • + 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;
    }