JavaScript Print shortest Path to Print a String on Screen Last Updated : 08 Nov, 2023 Comments Improve Suggest changes Like Article Like Report 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 entire input string in the correct order. In this article, we will see how to find the shortest path to print a string by using Javascript. Screen: A B C D EF G H I JK L M N OP Q R S TU V W X YZExample: Input: “HELLO” Output: Right Right OK Up Right Right OK Left Left Left Down Down OK OK Right Right Right OK The idea is to consider the screen as a 2D matrix of characters. Then we consider all characters of a given string one by one and print out the shortest path between the current character and the next character in the matrix. In order to find the shortest path, we consider the coordinates of the current character and the next character in the matrix. Based on the difference between x and y values of the current and next character’s coordinates, we move left, right, top, or bottom. i.e. If row difference is negative, we move up If row difference is positive, we move down If column difference is negative, we go left If column difference is positive, we go rightExample: In this Example, we will find shortest path to print a string on screen by using javascript. JavaScript // Function to print the shortest // possible path to type a given inputString function printShortestPathToTypeString(inputString) { // Initialize current position (0, 0) // and index for the inputString let currentIndex = 0; let currentX = 0, currentY = 0; // Loop through the inputString while (currentIndex < inputString.length) { // Calculate the next position coordinates let nextX = parseInt ((inputString[currentIndex].charCodeAt() - 'A'.charCodeAt()) / 5, 10); let nextY = (inputString[currentIndex].charCodeAt() - 'B'.charCodeAt() + 1) % 5; // Move Up while (currentX > nextX) { console.log("Up"); currentX--; } // Move Left while (currentY > nextY) { console.log("Left"); currentY--; } // Move Down while (currentX < nextX) { console.log("Down"); currentX++; } // Move Right while (currentY < nextY) { console.log("Right"); currentY++; } // Press OK to type the character console.log("OK"); currentIndex++; } } // Driver code let inputString = "HELLO"; printShortestPathToTypeString(inputString); OutputDown Right Right OK Up Right Right OK Left Left Left Down Down OK OK Right Right Right OK Time Complexity: O(n*n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article JavaScript Print shortest Path to Print a String on Screen A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-string Geeks Premier League 2023 +1 More Similar Reads How to print or output a String? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: Lang 3 min read How to read or input a string? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How 3 min read C# - Different Ways to Find All Substrings in a String Given a string as an input we need to find all the substrings present in the given string. Example: Input: geeks Output: g e e k s ge ee ek ks gee eek eks geek eeks geeks Input: ab Output: a b abMethod 1: Using Substring() method We can find all the substrings from the given string using the Substri 3 min read How to Read a File using Applet? In this article, we will learn how to read the content of the file using Java and display those contents using Applet. Approach UsedThe user should type the name of the file in the input field that asks for the filename in the output applet window. If the file is already present, it will display the 2 min read C# Program to Print Hello World Without Using WriteLine Hello World program is the most basic program of any programming language. It prints "Hello world" on the screen. In this article, we will display "Hello World" without using WriteLine Method. So to do this task we use the following methods: Console.OpenStandardOutput(): This method is used to acqui 2 min read Pattern of Strings Given a string S of length N, find the pattern of the strings as shown below in the examples. Examples: Input: S = "Geek"Output: Geek, Gee, Ge, GExplanation: Decrease one character after each line Input: S = "G*g" Output: G*g, G*, GExplanation: Decrease one character after each line Using two Nested 5 min read Java File Class toString() Method with Examples This method returns the pathname string of the File object is returned by the function toString() function. This method is the same as calling the Java IO getPath() method. Package view is as follows: --> java.io Package --> File Class --> toString() Method Syntax: public String toString() 1 min read 3 Ways for Traversal of Strings String Traversal operations refers to process of one character at a time. Often we start at the beginning, select each character in turn, do something to it, and continue until the end. Examples: Input: str = âGeeksforGeeksâOutput: G e e k s f o r G e e k s Input: str = âCoderâOutput: C o d e r Meth 4 min read Length of a String Given a string s, the task is to find the length of the string.Examples: Input: s = "abc"Output: 3Input: s = "GeeksforGeeks"Output: 13Input: s = ""Output: 0Using In-built methodsEvery programming language offers a built-in method as well to find the lengthC++// C++ program to find length // of a str 3 min read java.nio.file.Paths Class in Java java.nio.file.Paths class contains static methods for converting path string or URI into Path. Class declaration : public final class Paths extends ObjectMethods: MethodDescriptionget(String first, String... more) This method converts a path string, or a sequence of strings that when joined form a p 2 min read Like