title: Next Word in Lexicographic Order
body: <p><strong>Problem</strong></p>
  
  <p>There is a word given. We need to find the next word occurring in lexicographic order.For example, if word is <code>lkjihfg</code> then the next word would be <code>lkjihgf</code>.</p>
  
  <p>This is a problem at <strong>Hackerrank</strong> that you can see <a href="https://www.hackerrank.com/challenges/bigger-is-greater" rel="nofollow noreferrer">here</a>.
  The problem for reference:</p>
  
  <blockquote>
    <p>Complete the biggerIsGreater function in the editor below. It should
    return the smallest lexicographically higher string possible from the
    given string or no answer.</p>
  </blockquote>
  
  <p><strong>My effort</strong></p>
  
  <p>What i've tried was finding the maximum index(say <code>indx</code>) of word such that after it all the characters are non increasing.Then swapping the char at <code>indx</code> with the minimum char ahead that is greater than char at <code>indx</code>. Finally, reversing the string after <code>indx</code>.</p>
  
  <p><strong>Code</strong>   </p>
  
  <pre><code>def biggerIsGreater(w):
      ww = list(set(w))
      indx = -1
      l = [ord(ch)for ch in w]
      for i in range(len(l)-1):
          if l[i] &lt; l[i+1]:
              indx = i
          else:
              continue
      if indx == -1:
          return "no answer"
      j = len(l) - 1
      for x in range(j,indx,-1):
          if l[x] &gt; l[indx]:
              l[x], l[indx] = l[indx], l[x]
              break
      l[indx+1 : ] = l[len(l) - 1 : indx : -1]
      y = []
      for z in l:
          y.append(chr(z))
      ans = ''.join(y)
      return ans
  </code></pre>
  
  <p>The problem with this code is that it's not passing all the test cases because it's producing wrong results.</p>