Minimum time required to print given string from a circular container based on given conditions
Last Updated :
16 Jun, 2022
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 time
- Print the character in one unit time, and then move the pointer to the next index of the string
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:
- Calculate time required to reach character index from current pointer index in both directions:
- clockwise time is calculated by taking absolute difference of both indices
- anti-clockwise time is calculated by subtracting clockwise time from 26
- Minimum of both times obtained in the previous step is added to the answer
- Then to print the character, one unit time is also added to the answer
Finally print the answer obtained as the minimum time required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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 << ans;
}
int main()
{
string str = "zjpc" ;
minTime(str);
return 0;
}
|
Java
class GFG{
static void minTime(String word)
{
int ans = 0 ;
int curr = 0 ;
for ( int i = 0 ; i < word.length(); i++) {
int k = ( int )word.charAt(i) - 97 ;
int a = Math.abs(curr - k);
int b = 26 - Math.abs(curr - k);
ans += Math.min(a, b);
ans++;
curr = ( int )word.charAt(i) - 97 ;
}
System.out.print(ans);
}
public static void main(String[] args)
{
String str = "zjpc" ;
minTime(str);
}
}
|
Python3
def minTime(word):
ans = 0
curr = 0
for i in range ( len (word)):
k = ord (word[i]) - 97
a = abs (curr - k)
b = 26 - abs (curr - k)
ans + = min (a, b)
ans + = 1
curr = ord (word[i]) - 97
print (ans)
if __name__ = = '__main__' :
str = "zjpc"
minTime( str )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void minTime( string word)
{
int ans = 0;
int curr = 0;
for ( int i = 0; i < word.Length; i++) {
int k = ( int )word[i] - 97;
int a = Math.Abs(curr - k);
int b = 26 - Math.Abs(curr - k);
ans += Math.Min(a, b);
ans++;
curr = ( int )word[i] - 97;
}
Console.Write(ans);
}
public static void Main()
{
string str = "zjpc" ;
minTime(str);
}
}
|
Javascript
<script>
funs minTime(string word)
{
int ans = 0;
let curr = 0;
for (let i = 0; i < word.Length; i++) {
int k = word[i].charAt(0) - 'a' .charAt(0);
let a = Math.abs(curr - k);
let b = 26- Math.abs(curr - k);
ans += Math.min(a, b);
ans++;
curr = word[i].charAt(0)- 'a' .charAt(0);
}
document.write(ans);
}
let str= "zjpc" ;
minTime(str);
</script>
|
Time complexity: O(N), where N is the length of given string str
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...