Sort by

recency

|

994 Discussions

|

  • + 0 comments

    Here is my Python solution!

    def biggerIsGreater(w):
        if "".join(sorted(list(w), reverse=True)) == w:
            return "no answer"
        i1 = len(w) - 2
        while w[i1] >= w[i1 + 1]:
            i1 -= 1
        i2 = len(w) - 1
        while w[i2] <= w[i1]:
            i2 -= 1
        left = list(w)[:i1]
        changed = list(w[i1:])
        changed[0], changed[i2 - i1] = changed[i2 - i1], changed[0]
        middle = changed[0:1]
        right = changed[1:]
        right.sort()
        return "".join(left + middle + right)
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-bigger-is-greater-problem-solution.html

  • + 0 comments

    Lexicographical order is often known as alphabetical order when dealing with strings. A string is greater than another string if it comes later in a lexicographically sorted list.

    Given a word, create a new word by swapping some or all of its characters. This new word must meet two criteria:

    It must be greater than the original word It must be the smallest word that meets the first condition Example

    The next largest word is .

    Complete the function biggerIsGreater below to create and return the new string meeting the criteria. If it is not possible, return no answer.

    Function Description

    Complete the biggerIsGreater function in the editor below.

    biggerIsGreater has the following parameter(s):

    string w: a word Returns - string: the smallest lexicographically higher string possible or no answer

    Input Format

    The first line of input contains , the number of test cases. Each of the next lines contains .

    Constraints

    will contain only letters in the range ascii[a..z]. Sample Input 0

    5 ab bb hefg dhck dkhc Sample Output 0

    ba no answer hegf dhkc hcdk Explanation 0

    Test case 1: ba is the only string which can be made by rearranging ab. It is greater. Test case 2: It is not possible to rearrange bb and get a greater string. Test case 3: hegf is the next string greater than hefg. Test case 4: dhkc is the next string greater than dhck. Test case 5: hcdk is the next string greater than dkhc. Sample Input 1

    6 lmno dcba dcbb abdc abcd fedcbabcd Sample Output 1

    lmon no answer no answer acbd abdc fedcbabdc Contest ends in an hour Submissions: 0 Max Score: 35 Difficulty: Medium Rate This Challenge:

    More

  • + 0 comments

    Going from right to left, stop as soon as the predecessor character is greater than the current character. Then find the rightmost such such character and swap. Finally, sort the characters to the right of the current character. The effect of this procedure is to find the lowest possible lexicographic position which, when incremented by swapping, will be greater than the original string, and then to minimze all the lower positions. The resulting string is guaranteed to be lexicographically greater than the original, and any swap will be either greater than this string or smaller than the original string.

    def biggerIsGreater(w):
        w = list(w)
        right_end = len(w) - 1
        left, swapped = right_end - 1, False
        while not swapped and left > -1:
            right = left + 1
            if w[left] < w[right]:
                while right <= right_end and w[left] < w[right]:
                    right += 1
                w[left], w[right - 1], swapped = w[right - 1], w[left], True
            left -= 1
        if not swapped:
            return 'no answer'
        return ''.join(w[:left+2] + sorted(w[left+2:]))
    
  • + 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()
        }
    }