innovation

Turing Number Generator

A digit-only keyboard contains all 10 digits from e to 9 . They all exist in one line. Given a string of is digits illustrating how the keys are positioned. To type a digit, you start from index zero to the index of the target digit. It takes la – 131 milliseconds to move from index a to index b . Write a function to calculate the number of milliseconds needed to type a number with one finger. Example 1: Input: digits = “0123456789”, num:210 Output: 4 Example 2: Input: digits = “8459761203”, num = “5439” Output: 17 Constraints: • digits.length == 10 • digits contains each digit [0-9] exactly once in some order. • 1 <= num.length <= 104 • num(i) is digit. using C#

Share Prompt

Here’s a possible C# implementation of the function:

csharpCopy codepublic static int CalculateTypingTime(string digits, string num)
{
    int time = 0;
    int currentIndex = 0;
    foreach (char c in num)
    {
        int targetIndex = digits.IndexOf(c);
        time += Math.Abs(targetIndex - currentIndex);
        currentIndex = targetIndex;
    }
    return time;
}

The function takes in two parameters digits and num, representing the digit positions and the number to type, respectively. It initializes a variable time to keep track of the total typing time and a variable currentIndex to keep track of the current index of the finger.

The function iterates over each digit in num, and for each digit, it calculates the index of that digit in the digits string using the IndexOf method. It then calculates the absolute difference between the target index and the current index and adds it to the time variable. Finally, it updates the currentIndex variable to the target index for the next iteration.

The function returns the total time variable as the result.

You can call this function like this:

javaCopy codestring digits = "0123456789";
string num = "210";
int typingTime = CalculateTypingTime(digits, num);
Console.WriteLine(typingTime);

This will output 4, which is the expected result for the given input.

KhalidAhmed
Khalid Ahmed

Expert .NET Full-Stack Developer with 10+ years building scalable business applications. Proficient in C#, ASP.NET Core, Angular, SQL, and Azure Cloud. Strong background in SDLC, APIs, microservices, and DevOps. Delivers high-performance solutions aligned with business needs. Let’s innovate together!

Khalid Ahmed

Expert .NET Full-Stack Developer with 10+ years building scalable business applications. Proficient in C#, ASP.NET Core, Angular, SQL, and Azure Cloud. Strong background in SDLC, APIs, microservices, and DevOps. Delivers high-performance solutions aligned with business needs. Let’s innovate together!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button