# 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:Move the pointer one character anti-clockwise or clockwise in one unit timePrint the character in one unit time, and then move the pointer to the next index of the stringExample:Input: str = “zcd”Output: 8Explanation: The steps are as follows:Move the pointer anti-clockwise to ‘z’ in 1 secondType the character ‘z’ in 1 secondMove the pointer clockwise to ‘c’ in 3 secondsType the character ‘c’ in 1 secondMove the pointer clockwise to ‘d’ in 1 secondsType the character ‘d’ in 1 second.Input: str =”zjpc”Output: 34Explanation: The steps are as follows:Move the pointer anti-clockwise to ‘z’ in 1 secondType the character ‘z’ in 1 secondMove the pointer clockwise to ‘j’ in 10 secondsType the character ‘j’ in 1 secondMove the pointer clockwise to ‘p’ in 6 secondsType the character ‘p’ in 1 secondMove the pointer anti-clockwise to ‘c’ in 13 seconds.Approach: Below steps can be followed to solve the given problem:Calculate time required to reach character index from current pointer index in both directions:clockwise time is calculated by taking absolute difference of both indicesanti-clockwise time is calculated by subtracting clockwise time from 26Minimum of both times obtained in the previous step is added to the answerThen to print the character, one unit time is also added to the answerFinally print the answer obtained as the the minimum time required.Below is the implementation of the above approach:C++#include using namespace std;  void minTime(string word){      int ans = 0;              int curr = 0;      for (int i = 0; i < word.length(); i++) {                  int k = word[i] - 'a';                                  int a = abs(curr - k);                          int b = 26 - abs(curr - k);                          ans += min(a, b);                          ans++;          curr = word[i] - 'a';    }          cout