█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 2593

Problema 0x5c - Decir si hay una letra repetida

  • Autor: EduardoVega
  • Fecha: 2021-01-09 23:36:36 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.004 s 2 KBi
#2
Correcto
0.005 s 1 KBi
#3
Correcto
0.005 s 29 KBi
#4
Correcto
0.004 s 1 KBi
#5
Correcto
0.004 s 31 KBi
#6
Correcto
0.004 s 37 KBi
#7
Correcto
0.004 s 1 KBi
#8
Correcto
0.003 s 1 KBi
#9
Correcto
0.004 s 1 KBi
#10
Correcto
0.005 s 21 KBi
#11
Correcto
0.005 s 1 KBi
#12
Correcto
0.004 s 1 KBi
Puntos totales: 100 / 100

Código

#include <stdio.h>
#include <stdlib.h>

#define SIZE 50

struct node
{
    char key;
    struct node *next;
};

typedef struct node l_list;

struct hash_table 
{
    l_list **entries;    
};

typedef struct hash_table h_table;

int hash_function(char key)
{
    int hash_value = key % SIZE;
    
    return hash_value;    
}

h_table *create_table()
{
    h_table *new_table = malloc(sizeof(h_table));
    new_table->entries = malloc(sizeof(l_list *) * SIZE);
    int i;
    
    for(i = 0; i < SIZE; i++)
        new_table->entries[i] = NULL; 
    
    return new_table;    
}

l_list *create_node(char key)
{
    l_list *new_node = malloc(sizeof(l_list));
    new_node->key = key;
    new_node->next = NULL;

    return new_node;
}

int set_entry(h_table *hashtable, char key)
{
    int position = hash_function(key);
    
    if(hashtable->entries[position] != NULL)
        return -1;
    
    hashtable->entries[position] = create_node(key);
    return 0;
}

void delete_table(h_table *hashtable)
{
    int i = 0;
    
    for(; i < SIZE; i++)
    {
        if(hashtable->entries[i] != NULL)
            free(hashtable->entries[i]);
    }
}

int main()
{
    char input[50];
    int i = 0, check = 0;
    h_table *hashtable = create_table();
    
    scanf("%s", input);
    
    for(; input[i] != '\0'; i++)
    {
        check = set_entry(hashtable, input[i]);
        
        if(check == -1)
        {
            printf("yes");
            delete_table(hashtable);
            return 0;
        }
    }
    printf("no");
    delete_table(hashtable);
    return 0;
}