Swift Program to check whether a given string is Heterogram or not


Heterogram string is a string in which no alphabet occurs more than once. For example, “Sky with cloud” is a heterogram because in the string each alphabet occurred only once. Whereas “colorful balloons” is not a Heterogram because in the string some alphabet like o, u, l, etc appears more than once. Here we will execute different examples that will check whether the given string is heterogram or not using swift programming language.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Remove the white space from the string.

  • Step 3 − Create a separate array to store all the alphabets present in the input string.

  • Step 4 − Convert the array into set to store unique elements.

  • Step 5 − If the length of the set is equal to the number of input alphabets means each alphabet occurred once then the set is heterogram. Otherwise not.

  • Step 6 − Create a string and pass it into the function.

  • Step 7 − Print the output.

Example 1

Following Swift program to check whether a given string is heterogram or not.

import Foundation
import Glibc

// Function to check if the given string is heterogram or not
func CheckHeterogram(str: String) -> Bool {
   // Remove the white spaces from the string
   let nStr = str.replacingOccurrences(of: " ", with: "")
   var set = Set<Character>()
   for c in nStr {
      if set.contains(c) {
         return false
      } else {
         set.insert(c)
      }
   }
   return true
}

// Test case 1
let myString1 = "I love Ma"
print("Is String-'\(myString1)' is a Heterogram?:", CheckHeterogram(str: myString1)) 

// Test case 2
let myString2 = "My sheep color is black"
print("Is String-'\(myString2)' is a Heterogram?:", CheckHeterogram(str: myString2))

Output

Is String-'I love Ma' is a Heterogram?: true
Is String-'My sheep color is black' is a Heterogram?: false

Here in the above code, we create a function named CheckHeterogram(). In this function, we uses set to keep track of the encountered characters. Then iterate through each character of the input string and check if the character is already present in the set or not. If this function return false, then that means the string is not heterogram. If the function iterates through the entire string without finding any duplicates, then it true which means the string is heterogram.

Example 2

Following Swift program to check whether a given string is heterogram or not.

import Foundation
import Glibc

// Function to check if the given string is heterogram or not
func CheckHeterogram(str: String) -> Bool {
   let nStr = str.replacingOccurrences(of: " ", with: "")
   let lString = nStr.lowercased()
   let charArray = Array(lString)
   let uniqueChars = Set(charArray)
   return uniqueChars.count == charArray.count
}

// Test case 1
let myString1 = "Hey Suman"
print("Is String-'\(myString1)' is a Heterogram?:", CheckHeterogram(str: myString1)) 

// Test case 2
let myString2 = "I love Cooking Food"
print("Is String-'\(myString2)' is a Heterogram?:", CheckHeterogram(str: myString2))

Output

Is String-'Hey Suman' is a Heterogram?: true
Is String-'I love Cooking Food' is a Heterogram?: false

Here in the above code, we have two input strings. Now we create a function to check if the given string are heterogram or not. In this function, first we convert the input string to lowercase and then creates an array of characters from the string and then convert the array into the set for unique characters. Now this function return true if the uniqueChar count is equal to the characterArray count that means the string is heterogram. This function will return false if the uniqueChar count is not equal to the characterArray count that means the string is not heterogram

Conclusion

So this is how we can check if the given string is heterogram or not.

Updated on: 08-Feb-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements