Maximice el tiempo reemplazando ‘_’ en un formato de hora dado de 24 horas

Dada una string S que representa una hora en formato de 24 horas , con ‘_’ colocado en posiciones de algunos dígitos, la tarea es encontrar el tiempo máximo posible reemplazando los caracteres ‘_’ con cualquier dígito.

Ejemplos:

Entrada: S = “0_:4_”
Salida: 09:39
Explicación:  Reemplazar los caracteres S[1] y S[4] con ‘9’ modifica la string a “09:49”, que es el tiempo máximo posible.

Entrada: S = “__:__”
Salida: 23:59

 

Enfoque: el problema dado se puede resolver seleccionando con avidez los dígitos para cada ‘_’ presente en la string. Siga los pasos a continuación para resolver el problema: 

  • Si el carácter S[0] es igual a ‘_’ y S[1] es ‘_’ o es menor que 4 , entonces asigne ‘ 2 ‘ a S[0] . De lo contrario, asigne ‘1’ a S[0 ]. ] .
  • Si el carácter S[1] es igual a ‘_’ y S[0] es ‘2’, entonces asigne ‘3’ a S[1] . De lo contrario, asigne ‘ 9 ‘ a S[1] .
  • Si el carácter S[3] es igual a ‘_’ , entonces asigne ‘5’ a S[3] .
  • Si el carácter S[4] es igual a ‘_’ , entonces asigne ‘ 9 ‘ a S[4] .
  • Después de completar los pasos anteriores, imprima la string modificada S .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
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

Javascript

<script>
// javascript program for the above approach
 
      
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
 
     function maximumTime(str)
{
     var s = str.split("");
      
    // 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(var i = 0; i < s.length; i++)
        document.write(s[i]);
}
  
// Driver Code
    var S = "0_:4_";   
    maximumTime(S);
 
// This code is contributed by bunnyram19.
</script>

C

// C program for above approach
#include <stdio.h>
#include <string.h>
 
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
char* maximumTime(char 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';
        }
        else { // Otherwise, update s[0] = 1
            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 s; // Return the modified string
}
int main()
{
    char S[] = "0_:4_";
    printf("%s", maximumTime(S));
    return 0;
}
 
// This code is contributed by Tapesh (tapeshdua420)
Producción

09:49

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por shreyajaiswal3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *