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 Comment More infoAdvertise with us Next Article JavaScript Program to find Lexicographically next String N nikhilgarg527 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 More 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 JavaScript Print shortest Path to Print a String on Screen Given a screen with the English alphabet (A-Z) and a remote control with navigation keys (left, right, up, down), we have to determine the most efficient way to write a string using the remote. Starting from the top-left corner of the screen, we need to move through the alphabet to spell out the ent 3 min read JavaScript Program to Generate Number Sequence Find the nâth term in the Look-and-say (Or Count and Say) Sequence. The look-and-say sequence is the sequence of the below integers: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211How is the above sequence generated? nâth term is generated by reading (n-1)âth term.The first term is "1"Second t 4 min read Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 2 min read Java Program to Print all Unique Words of a String Java program to print all unique words present in the string. The task is to print all words occurring only once in the string. Illustration: Input : Welcome to Geeks for Geeks. Output : Welcome to for Input : Java is great.Python is also great. Output : Java Python also Methods: This can be done in 4 min read Like