📌  相关文章
📜  通过在给定的24小时格式时间内替换“ _”来最大程度地延长时间

📅  最后修改于: 2021-04-17 17:10:33             🧑  作者: Mango

给定一个以24小时格式表示时间的字符串S ,并将“ _”放在一些数字的位置,任务是通过用任何数字替换字符“ _”来找到最大可能的时间。

例子:

方法:可以通过贪婪地选择字符串每个‘_’的数字来解决给定的问题。请按照以下步骤解决问题:

  • 如果字符S [0]等于‘_’并且S [1]‘_’或小于4 ,则将’ 2 ‘分配给S [0] 否则,将‘1’分配给S [0]
  • 如果字符S [1]等于‘_’并且S [0]‘2’,则将‘3’分配给S [1] 。否则,将’ 9 ‘分配给S [1]
  • 如果字符S [3]等于‘_’ ,则将‘5’分配给S [3]
  • 如果字符S [4]等于‘_’ ,则将’ 9 ‘分配给S [4]
  • 完成上述步骤后,打印修改后的字符串S。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
string maximumTime(string s)
{
    // If the first character is '_'
    if (s[0] == '_') {
 
        // If s[1] is '_' or
        // s[1] is less than 4
        if ((s[1] == '_')
            || (s[1] >= '0'
                && s[1] < '4')) {
 
            // Update s[0] as 2
            s[0] = '2';
        }
 
        // Otherwise, update s[0] = 1
        else {
            s[0] = '1';
        }
    }
 
    // If s[1] is equal to '_'
    if (s[1] == '_') {
 
        // If s[0] is equal to '2'
        if (s[0] == '2') {
            s[1] = '3';
        }
 
        // Otherwise
        else {
            s[1] = '9';
        }
    }
 
    // If S[3] is equal to '_'
    if (s[3] == '_') {
        s[3] = '5';
    }
 
    // If s[4] is equal to '_'
    if (s[4] == '_') {
        s[4] = '9';
    }
 
    // Return the modified string
    return s;
}
 
// Driver Code
int main()
{
    string S = "0_:4_";
    cout << maximumTime(S);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
static void maximumTime(String str)
{
    char []s = str.toCharArray();
     
    // If the first character is '_'
    if (s[0] == '_')
    {
         
        // If s[1] is '_' or
        // s[1] is less than 4
        if ((s[1] == '_') ||
            (s[1] >= '0' && s[1] < '4'))
        {
             
            // Update s[0] as 2
            s[0] = '2';
        }
 
        // Otherwise, update s[0] = 1
        else
        {
            s[0] = '1';
        }
    }
 
    // If s[1] is equal to '_'
    if (s[1] == '_')
    {
         
        // If s[0] is equal to '2'
        if (s[0] == '2')
        {
            s[1] = '3';
        }
 
        // Otherwise
        else
        {
            s[1] = '9';
        }
    }
 
    // If S[3] is equal to '_'
    if (s[3] == '_')
    {
        s[3] = '5';
    }
 
    // If s[4] is equal to '_'
    if (s[4] == '_')
    {
        s[4] = '9';
    }
 
    // Print the modified string
    for(int i = 0; i < s.length; i++)
        System.out.print(s[i]);
}
 
// Driver Code
static public void main (String []args)
{
    String S = "0_:4_";
     
    maximumTime(S);
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the maximum
# time possible by replacing
# each '_' with any digit
def maximumTime(s):
     
    s = list(s)
    # If the first character is '_'
    if (s[0] == '_'):
         
        # If s[1] is '_' or
        # s[1] is less than 4
        if ((s[1] == '_') or (s[1] >= '0' and
                              s[1] < '4')):
                                   
            # Update s[0] as 2
            s[0] = '2'
 
        # Otherwise, update s[0] = 1
        else:
            s[0] = '1'
 
    # If s[1] is equal to '_'
    if (s[1] == '_'):
         
        # If s[0] is equal to '2'
        if (s[0] == '2'):
            s[1] = '3'
 
        # Otherwise
        else:
            s[1] = '9'
 
    # If S[3] is equal to '_'
    if (s[3] == '_'):
        s[3] = '5'
 
    # If s[4] is equal to '_'
    if (s[4] == '_'):
        s[4] = '9'
 
    # Return the modified string
    s = ''.join(s)
    return s
 
# Driver Code
if __name__ == '__main__':
     
    S = "0_:4_"
     
    print(maximumTime(S))
     
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
static void maximumTime(string str)
{
    char []s = str.ToCharArray();
     
    // If the first character is '_'
    if (s[0] == '_')
    {
         
        // If s[1] is '_' or
        // s[1] is less than 4
        if ((s[1] == '_') ||
            (s[1] >= '0' && s[1] < '4'))
        {
             
            // Update s[0] as 2
            s[0] = '2';
        }
 
        // Otherwise, update s[0] = 1
        else
        {
            s[0] = '1';
        }
    }
 
    // If s[1] is equal to '_'
    if (s[1] == '_')
    {
         
        // If s[0] is equal to '2'
        if (s[0] == '2')
        {
            s[1] = '3';
        }
 
        // Otherwise
        else
        {
            s[1] = '9';
        }
    }
 
    // If S[3] is equal to '_'
    if (s[3] == '_')
    {
        s[3] = '5';
    }
 
    // If s[4] is equal to '_'
    if (s[4] == '_')
    {
        s[4] = '9';
    }
 
    // Print the modified string
    for(int i = 0; i < s.Length; i++)
        Console.Write(s[i]);
}
 
// Driver Code
static public void Main ()
{
    string S = "0_:4_";
     
    maximumTime(S);
}
}
 
// This code is contributed by AnkThon


输出:
09:49

时间复杂度: O(1)
辅助空间: O(1)