JavaScript Program to find Lexicographically next String Last Updated : 29 Aug, 2024 Summarize Suggest changes Share 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 Advertise with us Next Article JavaScript Program to Search a Word in a 2D Grid of Characters N nikhilgarg527 Follow Similar Reads JavaScript Program to Find Lexicographically Next String In this article, we are going to learn about Lexicographically next string in JavaScript. A lexicographically next string is the immediate string in a sorted order that comes after a given string when considering character sequences. It's determined by rearranging characters to find the smallest cha 3 min read JavaScript Program to Compare Two Strings Lexicographically This JavaScript program compares two strings lexicographically, meaning it checks if one string comes before or after the other in alphabetical order. Lexicographical comparison is based on the Unicode value of each character in the strings. The program should return a positive value if the first st 2 min read JavaScript Program to Find Kâth Non-Repeating Character in String The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps 6 min read JavaScript Program for Printing Shortest Common Supersequence A Shortest Common Supersequence (SCS) is the shortest or smallest string that contains two given strings as a subsequence. It is a minimal combination of characters that includes all elements of both input strings. In this article, we will see different approaches for printing the shortest common su 8 min read JavaScript Program to Search a Word in a 2D Grid of Characters In this article, we will solve a problem in which you are given a grid, with characters arranged in a two-layout(2D). You need to check whether a given word is present in the grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match 7 min read Javascript Program To Find Longest Common Prefix Using Word By Word Matching Given a set of strings, find the longest common prefix. Examples:Input : {âgeeksforgeeksâ, âgeeksâ, âgeekâ, âgeezerâ}Output : "gee"Input : {"apple", "ape", "april"}Output : "ap"We start with an example. Suppose there are two strings- âgeeksforgeeksâ and âgeeksâ. What is the longest common prefix in 3 min read Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More Like