Sort by

recency

|

996 Discussions

|

  • + 0 comments

    `string biggerIsGreater(string w) { int index = 0; int decStart = -1; int swapIndex = -1; while (index < w.length()) { // Increasing if (index + 1 < w.length() && w[index] < w[index + 1]) { decStart = index + 1; swapIndex = index; } // Decreasing, we found a new minimum that is smaller else if (decStart != -1 && w[index] <= w[decStart] && w[index] > w[swapIndex]) { // If the new character is greater than the character to be swapping decStart = index; } index++; }

    if (decStart == -1) {
        return "no answer";
    }
    
    string descending = w.substr(decStart);
    reverse(descending.begin() + 1, descending.end());
    if (swapIndex + 1 < decStart) {
        reverse(w.begin() + swapIndex + 1, w.begin() + decStart);
    }
    w.erase(decStart);
    w.insert(swapIndex, descending);
    return w;`
    

    }

  • + 0 comments

    solution in scala

    def biggerIsGreater(w: String): String = {
      var i = w.length - 1
      val charMap = mutable.Map[Char, Int]()
      var replacePos: Option[Int] = None
      while(i >= 0 && replacePos.isEmpty){
        charMap(w(i)) = i
        val nextLargest = charMap
          .filter { case (k, _) => k > w(i) }
          .minByOption { case (k, _) => k }
        replacePos = nextLargest match{
          case None => {
            i = i - 1
            None
          }
          case Some(s) => Some(charMap(s._1))
        }
      }
      replacePos match{
        case None => "no answer"
        case Some(s) => {
          val switched = w.updated(s, w(i)).updated(i, w(s))
          // sort after the initial switch to efficiently arrive at the smallest possible substring
          val (prefix, suffix) = switched.splitAt(i + 1)
          val sortedSuffix = suffix.sorted
          prefix + sortedSuffix
        }
      }
    }
    
  • + 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