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

 Live Demo

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!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements