Sort by

recency

|

986 Discussions

|

  • + 0 comments

    Python solution

    def find_smallest_char_larger(s, idx):
        char_compare = s[idx]
        sorted_chars = sorted(s[(idx + 1):])
        for char in sorted_chars:
            if char > char_compare:
                i = s.index(char, idx)
                return i
        return None
        
    def biggerIsGreater(w):
        # Write your code here
        w_list = list(w)
        
        for idx in reversed(range(0, len(w) - 1)):
            char_i_to_swap = find_smallest_char_larger(w_list, idx)
            if char_i_to_swap is not None:
                w_list[char_i_to_swap], w_list[idx] = w_list[idx], w_list[char_i_to_swap]
                w_list[(idx + 1):] = sorted(w_list[(idx + 1):])
                return ''.join(w_list)
        
        return 'no answer'
    
  • + 0 comments

    Java

    public static String biggerIsGreater(String w) {
            char[] charArray = w.toCharArray();
            int n = charArray.length;
    
            // Step 1: Find the rightmost character that is smaller than its next character
            int index = -1;
            for (int i = n - 2; i >= 0; i--) {
                if (charArray[i] < charArray[i + 1]) {
                    index = i;
                    break;
                }
            }
    
            // If no such character exists, it's the last permutation
            if (index == -1) {
                return "no answer";
            }
    
            // Step 2: Find the smallest character on the right of `index` that is larger than charArray[index]
            int switchIndex = -1;
            for (int i = n - 1; i > index; i--) {
                if (charArray[i] > charArray[index]) {
                    switchIndex = i;
                    break;
                }
            }
    
            // Step 3: Swap the characters at `index` and `switchIndex`
            char temp = charArray[index];
            charArray[index] = charArray[switchIndex];
            charArray[switchIndex] = temp;
    
            // Step 4: Sort the substring after `index` to get the next lexicographical order
            Arrays.sort(charArray, index + 1, n);
    
            return new String(charArray);
        }
    
  • + 0 comments

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

    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. 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'. Swap those two letters. This gives us "hkdc". Bali real estate 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.

  • + 1 comment

    Python3

    def biggerIsGreater(w):
        # i is the index of the element on the right hand-side
        index = None
        for i in range(len(w)-1, -1, -1): 
            w_list = sorted(list(w[i:]))
            w_list.reverse()
            sorted_w = ""
            for char in w_list:
                sorted_w += char
            if sorted_w != w[i:]:
                index = i
                break
        
        if index is None:
            return "no answer"
        
        smallest_bigger = None
        for i in w[index:]:
            if i > w[index] and (smallest_bigger is None or i < smallest_bigger):
                smallest_bigger = i
        w_list = list(w[index + 1:])
        w_list.remove(smallest_bigger)
        w_list.append(w[index])
        w_list.sort()
        sorted_w = ""
        for char in w_list:
            sorted_w += char
        w = w[:index] + smallest_bigger + sorted_w
        return w
    
  • + 0 comments

    Tip: A nice observation is that once we swap our pivot position and the character most to the right that is greater than our pivot, we can reverse the rest of the string after the pivot (instead of sort).

    This is because while we were finding the character to swap, we already ensured that the elements to the right of the pivot ARE smaller jacqueline tortorice, so it's descending order. That's why reversing after the pivot point works.