TCHS-6-250

Problem Statement

     A digital clock displays time in the format "DD:DD:DD" (quotes for clarity only), where each D is a single digit, and each pair of contiguous digits represents either the hour, minute, or second (each of these three units will occur exactly once). Unfortunately, we do not know the ordering of the time units, so need to figure out the number of valid ways to interpret the displayed time.

The hour must be between 01 and 12, inclusive, the minute must be between 00 and 59, inclusive, and the second must be between 00 and 59, inclusive.
For example, "21:23:01" can be intepreted in two ways: "minute:second:hour" or "second:minute:hour".

Definition

    
Class: DigitalDisplay
Method: waysToInterpret
Parameters: String
Returns: int
Method signature: int waysToInterpret(String display)
(be sure your method is public)
    
 

Constraints

- display will contain exactly 8 characters.
- display will be formatted as "DD:DD:DD" (quotes for clarity only), where each D is a digit ('0'-'9').

Examples

0)  
    
"21:23:01"
 
Returns: 2
 
The example from the problem statement.
1)  
    
"00:00:00"
 
Returns: 0
 
There are no valid interpretations here because the hour must be at least 01.
2)  
    
"01:02:03"
 
Returns: 6
 
 
3)  
    
"59:59:01"
 
Returns: 2
 
 
4)  
    
"01:01:59"
 
Returns: 4
 
The four ways to interpret this are:
"hour:minute:second",
"hour:second:minute",
"minute:hour:second",
"second:hour:minute".
public class DigitalDisplay {

	public static int waysToInterpret(String s) {
		int a = Integer.parseInt(s.substring(0, 2));
		int b = Integer.parseInt(s.substring(3, 5));
		int c = Integer.parseInt(s.substring(6, 8));
		int count = 0;
		if (a >= 1 && a <= 12 && b >=0 && b <= 59 && c >=0 && c <= 59)
			count += 2;
		if (b >= 1 && b <= 12 && a >=0 && a <= 59 && c >=0 && c <= 59)
			count += 2;
		if (c >= 1 && c <= 12 && b >=0 && b <= 59 && a >=0 && a <= 59)
			count += 2;
		return count;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值