Sort by

recency

|

988 Discussions

|

  • + 0 comments

    If you're looking for a bigger, better outdoor experience, a tilting outdoor umbrella is a must! It offers shade where you need it most and adjusts easily to keep you cool all day long. Perfect for those sunny afternoons or casual outdoor gatherings. You'll definitely enjoy the comfort and convenience!

  • + 0 comments

    Perl solution:

    sub sorted_descending {
        my $arr = shift;
        for (my $i = 0; $i < scalar(@$arr) - 1; $i++) {
            return 0 if ($arr->[$i] lt $arr->[$i + 1]);
        }
        return 1;
    }
    
    sub biggerIsGreater {
        my $w = shift;
        
        my @arr = split("", $w);
        my $max = $arr[1];
        my $ind = 0;
        my @res;
        if (sorted_descending(\@arr)) {
            return "no answer";
        } else {
            my $k = $#arr - 1;
            while ($k >= 0 && $arr[$k] ge $arr[$k + 1]) {
                $k--;
            }
            
            my $l = $#arr; 
            while ($arr[$l] le $arr[$k]) {
                $l--;
            }
            
            ($arr[$k], $arr[$l]) = ($arr[$l], $arr[$k]);
            push(@res, @arr[0..$k], reverse(splice(@arr, $k+1, $#arr)));
            
            return join("", @res);
        }
    }
    
  • + 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.