Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to check whether a given string is Heterogram or not
A Heterogram is a string that contains no duplicate letters. Each character in the string appears exactly once. For example, words like "mobile", "cry", and "laptop" are heterograms because no letter is repeated.
Examples of Heterograms
mobile - No repeated letters cry - All unique letters laptop - Each letter appears once
Algorithm
The algorithm uses an integer array to track which letters have been encountered. For each character in the string −
-
Convert the character to an array index using
str[i] - 'a' -
Check if the character has been seen before (array value is 0)
-
If not seen, mark it as encountered; if already seen, return false
for (int i = 0; i < len; i++) {
if (val[str[i] - 'a'] == 0)
val[str[i] - 'a'] = 1;
else
return false;
}
Using Array-Based Approach
Example
using System;
public class HeterogramChecker {
static bool checkHeterogram(string str) {
int[] val = new int[26];
for (int i = 0; i < str.Length; i++) {
int index = str[i] - 'a';
if (val[index] == 0)
val[index] = 1;
else
return false;
}
return true;
}
public static void Main() {
string str1 = "mobile";
string str2 = "hello";
string str3 = "laptop";
Console.WriteLine($"'{str1}' is " + (checkHeterogram(str1) ? "a Heterogram" : "not a Heterogram"));
Console.WriteLine($"'{str2}' is " + (checkHeterogram(str2) ? "a Heterogram" : "not a Heterogram"));
Console.WriteLine($"'{str3}' is " + (checkHeterogram(str3) ? "a Heterogram" : "not a Heterogram"));
}
}
The output of the above code is −
'mobile' is a Heterogram 'hello' is not a Heterogram 'laptop' is a Heterogram
Using HashSet Approach
Example
using System;
using System.Collections.Generic;
public class HeterogramChecker2 {
static bool checkHeterogram(string str) {
HashSet<char> seenChars = new HashSet<char>();
foreach (char c in str) {
if (seenChars.Contains(c))
return false;
seenChars.Add(c);
}
return true;
}
public static void Main() {
string[] testStrings = {"cry", "programming", "desktop"};
foreach (string str in testStrings) {
Console.WriteLine($"'{str}' is " + (checkHeterogram(str) ? "a Heterogram" : "not a Heterogram"));
}
}
}
The output of the above code is −
'cry' is a Heterogram 'programming' is not a Heterogram 'desktop' is not a Heterogram
Using LINQ Approach
Example
using System;
using System.Linq;
public class HeterogramChecker3 {
static bool checkHeterogram(string str) {
return str.Length == str.Distinct().Count();
}
public static void Main() {
string[] words = {"world", "example", "unique"};
foreach (string word in words) {
bool isHeterogram = checkHeterogram(word);
Console.WriteLine($"'{word}' has {word.Length} characters, {word.Distinct().Count()} unique - " +
(isHeterogram ? "Heterogram" : "Not a Heterogram"));
}
}
}
The output of the above code is −
'world' has 5 characters, 5 unique - Heterogram 'example' has 7 characters, 6 unique - Not a Heterogram 'unique' has 6 characters, 5 unique - Not a Heterogram
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Array-based | O(n) | O(1) - fixed 26 elements | Lowercase letters only |
| HashSet | O(n) | O(k) - k unique characters | Any character set |
| LINQ | O(n) | O(k) - k unique characters | Concise, readable code |
Conclusion
A heterogram is a string with no repeated characters. The array-based approach is most efficient for lowercase letters, while HashSet provides flexibility for any character set. LINQ offers the most concise solution using Distinct().Count() to compare unique characters with string length.
