JavaScript Program to find Lexicographically next String Last Updated : 29 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how can we find the Lexicographically next string. Lexicographically next string refers to finding the string that follows a given string in a dictionary or alphabetical order.Examples: Input : testOutput : tesuExplanation : The last character 't' is changed to 'u'.Input : xyzOutput : xzzExplanation : Since we can't increase last character, we increment previous character.Input : zOutput : zaExplanation : If string is empty, we return ‘a’. If string contains all characters as ‘z’, we append ‘a’ at the end.Otherwise we find first character from end which is not z and increment it.ApproachIf the input string s is empty, it returns "a" as the next word.It finds the rightmost character in the string that is not 'z'.If all characters in the string are 'z', it appends 'a' at the end.If there are non-'z' characters in the string, it increments the rightmost non-'z' character by one.Example: This example shows the implementation of the above mentioned approach. JavaScript function nextWord(s) { // If string is empty. if (s == "") return "a"; // Find first character from right // which is not z. let i = s.length - 1; while (s[i] == 'z' && i >= 0) i--; // If all characters are 'z', append // an 'a' at the end. if (i == -1) s = s + 'a'; // If there are some non-z characters else s[i] = String.fromCharCode(s[i] .charCodeAt(0) + 1); return s.join(''); } // Driver code let str = "abcd".split(''); console.log(nextWord(str)); Outputabce Time Complexity: O(n)Auxiliary Space: O(1)String Manipulation Without ConversionIdea: Process the string from the end to the start, and handle cases where characters are 'z' by incrementing and managing the carry over.Steps:Process the String from End to Start: Traverse the string from the rightmost character to the leftmost.Handle 'z' Characters: If a 'z' is found, change it to 'a' and move to the next character to the left.Increment Non-'z' Characters: Once a non-'z' character is found, increment it by one and terminate further processing.Append 'a' if All Characters are 'z': If the string consists entirely of 'z's, append an 'a' at the end.Example: JavaScript function nextWord(s) { let str = s; let arr = [...str]; let n = arr.length; // Start from the end of the string for (let i = n - 1; i >= 0; i--) { if (arr[i] === 'z') { // Change 'z' to 'a' arr[i] = 'a'; } else { // Increment the character and exit arr[i] = String.fromCharCode(arr[i].charCodeAt(0) + 1); return arr.join(''); } } return 'a' + arr.join(''); } let str = "abcz"; console.log(nextWord(str)); Outputabda Create Quiz Comment N nikhilgarg527 Follow 0 Improve N nikhilgarg527 Follow 0 Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like