Comprobar si una string dada es Heterograma o no

Dada una string S. La tarea es verificar si la string dada es Heterograma o no. Un heterograma es una palabra, frase u oración en la que ninguna letra del alfabeto aparece más de una vez. 

Ejemplos: 

Input : S = "the big dwarf only jumps"
Output : Yes
Each alphabet in the string S is occurred
only once.

Input : S = "geeksforgeeks" 
Output : No
Since alphabet 'g', 'e', 'k', 's' occurred
more than once.

La idea es hacer una array hash de tamaño 26, inicializada en 0. Recorra cada alfabeto de la string dada y marque 1 en la posición correspondiente de la array hash si ese alfabeto se encuentra por primera vez, de lo contrario, devuelve falso.

A continuación se muestra la implementación de este enfoque: 

C++

// C++ Program to check whether the given string is Heterogram or not.
#include<bits/stdc++.h>
using namespace std;
 
bool isHeterogram(char s[], int n)
{
    int hash[26] = { 0 };
     
    // traversing the string.
    for (int i = 0; i < n; i++)
    {
        // ignore the space
        if (s[i] != ' ')
        {
            // if already encountered
            if (hash[s[i] - 'a'] == 0)
                hash[s[i] - 'a'] = 1;
                 
            // else return false.
            else
                return false;
        }
    }
     
    return true;
}
 
// Driven Program
int main()
{
    char s[] = "the big dwarf only jumps";
    int n = strlen(s);
    (isHeterogram(s, n))?(cout << "YES"):(cout << "NO");
    return 0;
}

Java

// Java Program to check whether the
// given string is Heterogram or not.
class GFG {
         
    static boolean isHeterogram(String s, int n)
    {
        int hash[] = new int[26];
         
        // traversing the string.
        for (int i = 0; i < n; i++)
        {
            // ignore the space
            if (s.charAt(i) != ' ')
            {
                // if already encountered
                if (hash[s.charAt(i) - 'a'] == 0)
                    hash[s.charAt(i) - 'a'] = 1;
                     
                // else return false.
                else
                    return false;
            }
        }
         
        return true;
    }
     
// Driver code
public static void main (String[] args)
{
    String s = "the big dwarf only jumps";
    int n = s.length();
     
    if(isHeterogram(s, n))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 code to check
# whether the given
# string is Heterogram
# or not.
 
def isHeterogram(s, n):
    hash = [0] * 26
     
    # traversing the
    # string.
    for i in range(n):
         
        # ignore the space
        if s[i] != ' ':
             
            # if already
            # encountered
            if hash[ord(s[i]) - ord('a')] == 0:
                hash[ord(s[i]) - ord('a')] = 1
             
            # else return false.
            else:
                return False
     
    return True
 
# Driven Code
s = "the big dwarf only jumps"
n = len(s)
 
print("YES" if isHeterogram(s, n) else "NO")
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Program to check whether the
// given string is Heterogram or not.
using System;
 
class GFG {
         
    static bool isHeterogram(string s, int n)
    {
        int []hash = new int[26];
         
        // traversing the string.
        for (int i = 0; i < n; i++)
        {
            // ignore the space
            if (s[i] != ' ')
            {
                // if already encountered
                if (hash[s[i] - 'a'] == 0)
                    hash[s[i] - 'a'] = 1;
                     
                // else return false.
                else
                    return false;
            }
        }
         
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
        string s = "the big dwarf only jumps";
        int n = s.Length;
         
        if(isHeterogram(s, n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by Vt_m.

PHP

<?php
// PHP Program to check
// whether the given string
// is Heterogram or not.
 
function isHeterogram($s, $n)
{
    $hash = array();
    for($i = 0; $i < 26; $i++)
        $hash[$i] = 0;
     
    // traversing the string.
    for ($i = 0; $i < $n; $i++)
    {
        // ignore the space
        if ($s[$i] != ' ')
        {
            // if already encountered
            if ($hash[ord($s[$i]) -
                      ord('a')] == 0)
                $hash[ord($s[$i]) -
                      ord('a')] = 1;
                 
            // else return false.
            else
                return false;
        }
    }    
    return true;
}
 
// Driven Code
$s = "the big dwarf only jumps";
$n = strlen($s);
if (isHeterogram($s, $n))
    echo ("YES");
else
    echo ("NO");
     
// This code is contributed by
// Manish Shaw(manishshaw1)
?>

Javascript

<script>
 
// Javascript program to check whether
// the given string is Heterogram or not.
function isHeterogram(s, n)
{
    var hash = Array(26).fill(0);
     
    // Traversing the string.
    for(var i = 0; i < n; i++)
    {
         
        // Ignore the space
        if (s[i] != ' ')
        {
             
            // If already encountered
            if (hash[s[i].charCodeAt(0) -
                      'a'.charCodeAt(0)] == 0)
                hash[s[i].charCodeAt(0) -
                      'a'.charCodeAt(0)] = 1;
                 
            // Else return false.
            else
                return false;
        }
    }
    return true;
}
 
// Driver code
var s = "the big dwarf only jumps";
var n = s.length;
 
(isHeterogram(s, n)) ? (document.write("YES")) :
                       (document.write("NO"));
                        
// This code is contributed by rutvik_56
 
</script>
Producción

YES

Complejidad temporal: O(N)
Espacio auxiliar: O(26)

Publicación traducida automáticamente

Artículo escrito por anuj0503 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 *