Open In App

Minimum time required to print given string from a circular container based on given conditions

Given a circular container consisting of lowercase alphabets from ‘a’ to ‘z’, a pointer pointing initially at alphabet ‘a’, and a string str, the task is to find the minimum time required to print the given string str from the circular container, based on following operations that can be performed on every character:

Example:



Input: str = “zcd”
Output: 8
Explanation: The steps are as follows:

  • Move the pointer anti-clockwise to ‘z’ in 1 second
  • Type the character ‘z’ in 1 second
  • Move the pointer clockwise to ‘c’ in 3 seconds
  • Type the character ‘c’ in 1 second
  • Move the pointer clockwise to ‘d’ in 1 seconds
  • Type the character ‘d’ in 1 second.

Input: str =”zjpc”
Output: 34
Explanation: The steps are as follows:



  • Move the pointer anti-clockwise to ‘z’ in 1 second
  • Type the character ‘z’ in 1 second
  • Move the pointer clockwise to ‘j’ in 10 seconds
  • Type the character ‘j’ in 1 second
  • Move the pointer clockwise to ‘p’ in 6 seconds
  • Type the character ‘p’ in 1 second
  • Move the pointer anti-clockwise to ‘c’ in 13 seconds.
 

Approach: Below steps can be followed to solve the given problem:

Finally print the answer obtained as the minimum time required.

Below is the implementation of the above approach:




// C++ implementation for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum time to
// print all characters in the string
void minTime(string word)
{
 
    int ans = 0;
 
    // Current element where the
    // pointer is pointing
    int curr = 0;
 
    for (int i = 0; i < word.length(); i++) {
 
        // Find index of that element
        int k = word[i] - 'a';
 
        // Calculate absolute difference
        // between pointer index and character
        // index as clockwise distance
        int a = abs(curr - k);
 
        // Subtract clockwise time from
        // 26 to get anti-clockwise time
        int b = 26 - abs(curr - k);
 
        // Add minimum of both times to
        // the answer
        ans += min(a, b);
 
        // Add one unit time to print
        // the character
        ans++;
 
        curr = word[i] - 'a';
    }
 
    // Print the final answer
    cout << ans;
}
 
// Driver code
int main()
{
    // Given string word
    string str = "zjpc";
 
    // Function call
    minTime(str);
    return 0;
}




// Java implementation for the above approach
class GFG{
 
// Function to calculate minimum time to
// print all characters in the string
static void minTime(String word)
{
 
    int ans = 0;
 
    // Current element where the
    // pointer is pointing
    int curr = 0;
 
    for (int i = 0; i < word.length(); i++) {
 
        // Find index of that element
        int k = (int)word.charAt(i) - 97;
 
        // Calculate absolute difference
        // between pointer index and character
        // index as clockwise distance
        int a = Math.abs(curr - k);
 
        // Subtract clockwise time from
        // 26 to get anti-clockwise time
        int b = 26 - Math.abs(curr - k);
 
        // Add minimum of both times to
        // the answer
        ans += Math.min(a, b);
 
        // Add one unit time to print
        // the character
        ans++;
 
        curr = (int)word.charAt(i) - 97;
    }
 
    // Print the final answer
    System.out.print(ans);
}
 
// Driver code
public static void main(String[] args)
{
   
    // Given string word
    String str = "zjpc";
 
    // Function call
    minTime(str);
}
 
}
 
// This code is contributed by AnkThon




# Python 3 implementation for the above approach
 
# Function to calculate minimum time to
# print all characters in the string
def minTime(word):
    ans = 0
 
    # Current element where the
    # pointer is pointing
    curr = 0
 
    for i in range(len(word)):
        # Find index of that element
        k = ord(word[i]) - 97
 
        # Calculate absolute difference
        # between pointer index and character
        # index as clockwise distance
        a = abs(curr - k)
 
        # Subtract clockwise time from
        # 26 to get anti-clockwise time
        b = 26 - abs(curr - k)
 
        # Add minimum of both times to
        # the answer
        ans += min(a, b)
 
        # Add one unit time to print
        # the character
        ans += 1
 
        curr = ord(word[i]) - 97
 
    # Print the final answer
    print(ans)
 
# Driver code
if __name__ == '__main__':
    # Given string word
    str = "zjpc"
 
    # Function call
    minTime(str)
     
    # This code is contributed by SURENDRA_GANGWAR.




// C# implementation for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate minimum time to
// print all characters in the string
static void minTime(string word)
{
 
    int ans = 0;
 
    // Current element where the
    // pointer is pointing
    int curr = 0;
 
    for (int i = 0; i < word.Length; i++) {
 
        // Find index of that element
        int k = (int)word[i] - 97;
 
        // Calculate absolute difference
        // between pointer index and character
        // index as clockwise distance
        int a = Math.Abs(curr - k);
 
        // Subtract clockwise time from
        // 26 to get anti-clockwise time
        int b = 26 - Math.Abs(curr - k);
 
        // Add minimum of both times to
        // the answer
        ans += Math.Min(a, b);
 
        // Add one unit time to print
        // the character
        ans++;
 
        curr = (int)word[i] - 97;
    }
 
    // Print the final answer
    Console.Write(ans);
}
 
// Driver code
public static void Main()
{
    // Given string word
    string str = "zjpc";
 
    // Function call
    minTime(str);
}
}
 
// This code is contributed by ipg2016107.




<script>
        // JavaScript Program to implement
        // the above approach
 
    // Function to calculate minimum time to
// print all characters in the string
funs minTime(string word)
{
  
    int ans = 0;
  
    // Current element where the
    // pointer is pointing
    let curr = 0;
  
    for (let i = 0; i < word.Length; i++) {
  
        // Find index of that element
        int k = word[i].charAt(0) - 'a'.charAt(0);
  
        // Calculate absolute difference
        // between pointer index and character
        // index as clockwise distance
        let a = Math.abs(curr - k);
  
        // Subtract clockwise time from
        // 26 to get anti-clockwise time
        let b = 26- Math.abs(curr - k);
  
        // Add minimum of both times to
        // the answer
        ans += Math.min(a, b);
  
        // Add one unit time to print
        // the character
        ans++;
  
        curr = word[i].charAt(0)- 'a'.charAt(0);
    }
  
    // Print the final answer
      document.write(ans);
}
 
        // Driver Code
 
        // Given Input
         let str="zjpc";
 
        // Function Call
        minTime(str);
 
// This code is contributed by dwivediyash
    </script>

 
 

Output
34

 

Time complexity: O(N), where N is the length of given string str
Auxiliary Space: O(1)

 


Article Tags :