Sort by

recency

|

990 Discussions

|

  • + 0 comments

    RUST:

    fn biggerIsGreater(w: &str) -> String {
        let length = w.len();
        let mut temp = w.chars().collect::<Vec<char>>();
        let mut i = None; //
        let mut idx: Option<usize> = None;
        let mut smallest = 'z';
    
        for a in (0..length - 1).rev() {
            if temp[a] < temp[a + 1] {
                i = Some(a);
                break;
        }
    }
        if let Some(i) = i {
        let mut j = i + 1;
        for k in i + 1..length {
        if temp[k] > temp[i] && (j == i + 1 || temp[k] < temp[j]) {
            idx = Some(k);
        }
    }
            if let Some(j) = idx {
                temp.swap(i, j);
                temp[i + 1..].sort();
            }
            temp.iter().collect()
        } else {
            "no answer".into()
        }
    }
    
  • + 0 comments

    It's just The Narayana Panditha's algorithm - indian mathematician from XIV century

  • + 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'