Difference between two given times
Last Updated :
25 Sep, 2022
Given two times in string “HH:MM” format. Here ‘H’ shows hours and ‘M’ shows minutes. You have to find the difference in the same string format between these two strings. But both given strings should follow these cases.
1) Time 1 will be less than or equal to time2
2) Hours will be possible from 0 to 23.
3) Minutes will be possible from 0 to 59
Examples:
Input : s1 = "14:00";
s2 = "16:45";
Output : result = "2:45"
Input : s1 = "1:04"
s2 = "13:05"
Output : result = "12:01"
The idea is to first remove colon. After removing colon, hour difference can be computed using simple mathematics.
C++
#include <bits/stdc++.h>
using namespace std;
int removeColon(string s)
{
if (s.size() == 4)
s.replace(1, 1, "" );
if (s.size() == 5)
s.replace(2, 1, "" );
return stoi(s);
}
string diff(string s1, string s2)
{
int time1 = removeColon(s1);
int time2 = removeColon(s2);
int hourDiff = time2 / 100 - time1 / 100 - 1;
int minDiff = time2 % 100 + (60 - time1 % 100);
if (minDiff >= 60) {
hourDiff++;
minDiff = minDiff - 60;
}
string res = to_string(hourDiff) + ':' + to_string(minDiff);
return res;
}
int main()
{
string s1 = "14:00" ;
string s2 = "16:45" ;
cout << diff(s1, s2) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int removeColon(String s)
{
if (s.length() == 4 )
s = s.substring( 0 , 1 ) + s.substring( 2 );
if (s.length() == 5 )
s = s.substring( 0 , 2 ) + s.substring( 3 );
return Integer.valueOf(s);
}
static String diff(String s1, String s2)
{
int time1 = removeColon(s1);
int time2 = removeColon(s2);
int hourDiff = time2 / 100 - time1 / 100 - 1 ;
int minDiff = time2 % 100 + ( 60 - time1 % 100 );
if (minDiff >= 60 ) {
hourDiff++;
minDiff = minDiff - 60 ;
}
String res = String.valueOf(hourDiff) + ':' + String.valueOf(minDiff);
return res;
}
public static void main(String args[])
{
String s1 = "14:00" ;
String s2 = "16:45" ;
System.out.println(diff(s1, s2));
}
}
|
Python3
def removeColon(s):
if ( len (s) = = 4 ):
s = s[: 1 ] + s[ 2 :]
if ( len (s) = = 5 ):
s = s[: 2 ] + s[ 3 :]
return int (s)
def diff(s1, s2):
time1 = removeColon(s1)
time2 = removeColon(s2)
hourDiff = time2 / / 100 - time1 / / 100 - 1 ;
minDiff = time2 % 100 + ( 60 - time1 % 100 )
if (minDiff > = 60 ):
hourDiff + = 1
minDiff = minDiff - 60
res = str (hourDiff) + ':' + str (minDiff)
return res
s1 = "14:00"
s2 = "16:45"
print (diff(s1, s2))
|
C#
using System;
class GFG {
static int removeColon( string s)
{
if (s.Length == 4)
s = s.Substring(0, 1) + s.Substring(2);
if (s.Length == 5)
s = s.Substring(0, 2) + s.Substring(3);
return Convert.ToInt32(s);
}
static string diff( string s1, string s2)
{
int time1 = removeColon(s1);
int time2 = removeColon(s2);
int hourDiff = time2 / 100 - time1 / 100 - 1;
int minDiff = time2 % 100 + (60 - time1 % 100);
if (minDiff >= 60) {
hourDiff++;
minDiff = minDiff - 60;
}
string res = Convert.ToString(hourDiff) + ":"
+ Convert.ToString(minDiff);
return res;
}
public static void Main( string [] args)
{
string s1 = "14:00" ;
string s2 = "16:45" ;
Console.WriteLine(diff(s1, s2));
}
}
|
Javascript
<script>
function removeColon( s)
{
if (s.length == 4)
s= s.replace( ":" , "" );
if (s.length == 5)
s= s.replace( ":" , "" );
return parseInt(s);
}
function diff( s1, s2)
{
time1 = removeColon(s1);
time2 = removeColon(s2);
hourDiff = parseInt(time2 / 100 - time1 / 100 - 1);
minDiff = parseInt(time2 % 100 + (60 - time1 % 100));
if (minDiff >= 60) {
hourDiff++;
minDiff = minDiff - 60;
}
res = (hourDiff).toString() + ':' + (minDiff).toString();
return res;
}
s1 = "14:00" ;
s2 = "16:45" ;
console.log(diff(s1, s2));
</script>
|
Output:
2:45
Time complexity: O(1), as the length of the string will be either 4 or 5, the operations performed by stoi and replace will be considered to be constant.
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...