简体   繁体   English

按字典顺序排列的下一个单词

[英]Next Word in Lexicographic Order

Problem问题

There is a word given.有一句话给。 We need to find the next word occurring in lexicographic order.For example, if word is lkjihfg then the next word would be lkjihgf .我们需要找到按字典顺序出现的下一个单词。例如,如果单词是lkjihfg则下一个单词将是lkjihgf

This is a problem at Hackerrank that you can see here .这是Hackerrank 的一个问题,你可以在这里看到。 The problem for reference:问题供参考:

Complete the biggerIsGreater function in the editor below.在下面的编辑器中完成 bigIsGreater 函数。 It should return the smallest lexicographically higher string possible from the given string or no answer.它应该返回给定字符串中可能的最小的字典序较高的字符串或没有答案。

My effort我的努力

What i've tried was finding the maximum index(say indx ) of word such that after it all the characters are non increasing.Then swapping the char at indx with the minimum char ahead that is greater than char at indx .我尝试过的是找到 word 的最大索引(比如indx ),以便在它之后所有字符都不会增加。然后将indx处的 char 与前面的最小字符交换,该最小字符大于indx char 。 Finally, reversing the string after indx .最后,在indx之后反转字符串。

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] < 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] > 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

The problem with this code is that it's not passing all the test cases because it's producing wrong results.这段代码的问题在于它没有通过所有的测试用例,因为它产生了错误的结果。

The good thing about your solution is that it has a good time complexity - O(n) , where n is a size of the input string.您的解决方案的好处是它具有很好的时间复杂度 - O(n) ,其中n是输入字符串的大小。 That's why you don't have any timeout errors.这就是为什么您没有任何超时错误的原因。

The problem with this code is, however is not all the test cases are validating.这段代码的问题是,并不是所有的测试用例都在验证。

That's so because you've missed one important case in your loop below:之所以如此,是因为您错过了以下循环中的一个重要案例:

for x in range(j,indx,-1):
    if l[x] >= l[indx]:
        l[x], l[indx] = l[indx], l[x]
        break

Consider a case like 5 4 6 4 .考虑像5 4 6 4这样的情况。 So, your indx is 2 and because of your condition l[x] >= l[indx] you will replace l[2] = 4 with l[0] = 4 .因此,您的indx2并且由于您的条件l[x] >= l[indx]您将用l[0] = 4替换l[2] = 4 l[0] = 4 This way, your next word in lexicographical order won't change and you'll get a wrong result 5 4 4 6 even though it must be 5 6 4 4 .这样,您按字典顺序排列的下一个单词不会改变,并且您会得到错误的结果5 4 4 6即使它必须是5 6 4 4

So, if you make your condition stricter > and not >= because you actually need a LARGER character from the left then the solution will be working correctly.因此,如果您使条件更严格>而不是>=因为您实际上需要左侧的更大字符,那么解决方案将正常工作。

Hence, just change your condition to > :因此,只需将您的条件更改为>

for x in range(j,indx,-1):
    if l[x] > l[indx]: # fix the sign here
        l[x], l[indx] = l[indx], l[x]
        break

I tested your code with the fix and it passed all the tests.我使用修复程序测试了您的代码,它通过了所有测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM