Check whether a given string is Heterogram or not
Last Updated :
28 Jul, 2022
Given a string S. The task is to check whether a the given string is Heterogram or not. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once.
Examples:
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.
The idea is to make a hash array of size 26, initialised to 0. Traverse each alphabet of the given string and mark 1 in the corresponding hash array position if that alphabet is encounter first time, else return false.
Below is the implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isHeterogram( char s[], int n)
{
int hash[26] = { 0 };
for ( int i = 0; i < n; i++)
{
if (s[i] != ' ' )
{
if (hash[s[i] - 'a' ] == 0)
hash[s[i] - 'a' ] = 1;
else
return false ;
}
}
return true ;
}
int main()
{
char s[] = "the big dwarf only jumps" ;
int n = strlen (s);
(isHeterogram(s, n))?(cout << "YES" ):(cout << "NO" );
return 0;
}
|
Java
class GFG {
static boolean isHeterogram(String s, int n)
{
int hash[] = new int [ 26 ];
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) != ' ' )
{
if (hash[s.charAt(i) - 'a' ] == 0 )
hash[s.charAt(i) - 'a' ] = 1 ;
else
return false ;
}
}
return true ;
}
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" );
}
}
|
Python3
def isHeterogram(s, n):
hash = [ 0 ] * 26
for i in range (n):
if s[i] ! = ' ' :
if hash [ ord (s[i]) - ord ( 'a' )] = = 0 :
hash [ ord (s[i]) - ord ( 'a' )] = 1
else :
return False
return True
s = "the big dwarf only jumps"
n = len (s)
print ( "YES" if isHeterogram(s, n) else "NO" )
|
C#
using System;
class GFG {
static bool isHeterogram( string s, int n)
{
int []hash = new int [26];
for ( int i = 0; i < n; i++)
{
if (s[i] != ' ' )
{
if (hash[s[i] - 'a' ] == 0)
hash[s[i] - 'a' ] = 1;
else
return false ;
}
}
return true ;
}
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" );
}
}
|
PHP
<?php
function isHeterogram( $s , $n )
{
$hash = array ();
for ( $i = 0; $i < 26; $i ++)
$hash [ $i ] = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] != ' ' )
{
if ( $hash [ord( $s [ $i ]) -
ord( 'a' )] == 0)
$hash [ord( $s [ $i ]) -
ord( 'a' )] = 1;
else
return false;
}
}
return true;
}
$s = "the big dwarf only jumps" ;
$n = strlen ( $s );
if (isHeterogram( $s , $n ))
echo ( "YES" );
else
echo ( "NO" );
?>
|
Javascript
<script>
function isHeterogram(s, n)
{
var hash = Array(26).fill(0);
for ( var i = 0; i < n; i++)
{
if (s[i] != ' ' )
{
if (hash[s[i].charCodeAt(0) -
'a' .charCodeAt(0)] == 0)
hash[s[i].charCodeAt(0) -
'a' .charCodeAt(0)] = 1;
else
return false ;
}
}
return true ;
}
var s = "the big dwarf only jumps" ;
var n = s.length;
(isHeterogram(s, n)) ? (document.write( "YES" )) :
(document.write( "NO" ));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(26)
Share your thoughts in the comments
Please Login to comment...