📌  相关文章
📜  检查给定字符串是否为 Heterogram

📅  最后修改于: 2022-05-13 01:57:07.272000             🧑  作者: Mango

检查给定字符串是否为 Heterogram

给定一个字符串S 。任务是检查给定的字符串是否是 Heterogram。异形是一个单词、短语或句子,其中字母表中的字母不超过一次。

例子:

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.

这个想法是创建一个大小为 26 的哈希数组,初始化为 0。遍历给定字符串的每个字母表,如果第一次遇到该字母表,则在相应的哈希数组位置标记 1,否则返回 false。

下面是这种方法的实现:

C++
// C++ Program to check whether the given string is Heterogram or not.
#include
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


Javascript


输出:

YES

时间复杂度: O(N)

辅助空间: O(26)