Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to check whether a given string is Heterogram or not
Heterogram for a string means the string isn’t having duplicate letters. For example −
Mobile Cry Laptop
Loop through each word of the string until the length of the string −
for (int i = 0; i < len; i++) {
if (val[str[i] - 'a'] == 0)
val[str[i] - 'a'] = 1;
else
return false;
}
Above, len is the length of the string.
Let us see the complete code −
Example
using System;
public class GFG {
static bool checkHeterogram(string str, int len) {
int []val = new int[26];
for (int i = 0; i < len; i++) {
if (val[str[i] - 'a'] == 0)
val[str[i] - 'a'] = 1;
else
return false;
}
return true;
}
public static void Main () {
string str = "mobile";
// length of the entered string
int len = str.Length;
if(checkHeterogram(str, len))
Console.WriteLine("String is Heterogram!");
else
Console.WriteLine("String is not a Heterogram!");
}
}
Output
String is Heterogram!
Advertisements