Created
May 6, 2020 20:13
-
-
Save jatinsharrma/52a5ecb3daf1d0f75c70416b586afa69 to your computer and use it in GitHub Desktop.
Find the next word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /////////////////////////////////////////////// | |
| # -----------------Next word--------------------- | |
| #/////////////////////////////////////////////// | |
| ''' | |
| Question : You are given a word, you have to find the least greatest | |
| word to the given word. If you can not find return None | |
| Example : given : 'acdb' | |
| output : 'adbc' | |
| Example : given : 'aaaa' # for this u not make a greater work | |
| output : None | |
| ''' | |
| ''' | |
| Logic : | |
| Here initially we are finding the place where we have to make change | |
| Then finding the element which will take it's place | |
| After this sorting the remaining word to the right of found place. | |
| Example : given : 'acdb' | |
| We start from the last. Compare last and seconds last characters. | |
| Consider given [i] > given [i+1] | |
| 1. if true : which means we have a case 'ba' we can not make a | |
| greater word because it is already is in its greatest form | |
| 2. if false : which means we have a case like 'ab' here we can change say | |
| using point 'a' we can make a greater word | |
| Lets apply this : | |
| We will start with index 2 | |
| Comparing index 2 & index 3 : 'd' > 'b' # So it's true | |
| Now compare 1 & 2 : 'c' > 'd' # it is false | |
| So we get a point where we can make changes i.e index 1 | |
| Now we will find the greater character from the right side. | |
| That will be 'd' and replace it | |
| So we get 'adcb' | |
| Now sort characters after index | |
| We get 'adbc' | |
| ''' | |
| def helper(w): | |
| l = len(w) - 2 | |
| while l > 0 and w[l] > w[l+1]: | |
| l -= 1 | |
| if l < 0 : | |
| return False | |
| n = len(w) - 1 | |
| while w[n] < w[l]: | |
| n -= 1 | |
| w[l],w[n] = w[n],w[l] | |
| w[l+1:] = sorted(w[l+1:]) | |
| return w | |
| def nextBig(w): | |
| x = helper(list(w)) | |
| if x: | |
| return "".join(x) | |
| return 'no answer' | |
| print(nextBig('acdb')) | |
| ''' | |
| Logic: | |
| Note : this logic is not efficient | |
| Here i am finding permutations from the last | |
| Example: given 'acdf' | |
| I am starting with last 2 index making permutations | |
| and trying my luck. After that last 3 index again | |
| finding permutations. If i get i return the value | |
| ''' | |
| # def helper(w,i): | |
| # found = 'z'* len(w) | |
| # for j in range(i,len(w)): | |
| # new = w[:i] + w[j] + "".join(sorted(w[i:j]+w[j+1:])) | |
| # if found > new and new > w: | |
| # found = new | |
| # return found if found != 'z'*len(w) else False | |
| # def nextBig(w): | |
| # for i in range(len(w)-2,-1,-1): | |
| # x = helper(w,i) | |
| # if x: | |
| # return x | |
| # return 'no answer' | |
| # print(nextBig('acdb')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment