Check if a string is Isogram or not using Hash-Map

To solve the problem follow the below idea:

In this, the count of characters of the string is stored in the hashmap, and wherever it is found to be greater than 1 for any char, return false else return true at the end

Follow the given steps to solve the problem:

  • Declare a hashmap to store the count of the characters of the string
  • Traverse the string
    • Increase the count of the current character in the hashmap
    • If the count of the current character is greater than one then return false
  • Return true

Below is the implementation of the above approach:

C++




// CPP code to check string is isogram or not
#include <bits/stdc++.h>
 
using namespace std;
 
// function to check isogram
bool check_isogram(string str)
{
 
    int length = str.length();
    int mapHash[26] = { 0 };
 
    // loop to store count of chars and check if it is
    // greater than 1
    for (int i = 0; i < length; i++) {
        mapHash[str[i] - 'a']++;
 
        // if count > 1, return false
        if (mapHash[str[i] - 'a'] > 1) {
            return false;
        }
    }
 
    return true;
}
 
// Driver code
int main()
{
    string str = "geeks";
    string str2 = "computer";
 
    // checking str as isogram
    if (check_isogram(str)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }
 
    // checking str2 as isogram
    if (check_isogram(str2)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }
 
    return 0;
}


Java




// Java code to check string is isogram or not
import java.io.*;
 
class GFG {
 
    // function to check isogram
    static boolean check_isogram(String str)
    {
 
        int length = str.length();
        int mapHash[] = new int[26];
 
        // loop to store count of chars and
        // check if it is greater than 1
        for (int i = 0; i < length; i++) {
            mapHash[str.charAt(i) - 'a']++;
 
            // if count > 1, return false
            if (mapHash[str.charAt(i) - 'a'] > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeks";
        String str2 = "computer";
 
        // checking str as isogram
        if (check_isogram(str))
            System.out.println("True");
        else
            System.out.println("False");
 
        // checking str2 as isogram
        if (check_isogram(str2))
            System.out.println("True");
        else
            System.out.println("False");
    }
}
 
// This code contributed by Rajput-Ji


Python3




# Python3 code to check string is isogram or not
 
# function to check isogram
 
 
def check_isogram(string):
 
    length = len(string)
    mapHash = [0] * 26
 
    # loop to store count of chars
    # and check if it is greater than 1
    for i in range(length):
 
        mapHash[ord(string[i]) - ord('a')] += 1
 
        # if count > 1, return false
        if (mapHash[ord(string[i]) - ord('a')] > 1):
 
            return False
 
    return True
 
 
# Driver code
if __name__ == "__main__":
 
    string = "geeks"
    string2 = "computer"
 
    # checking str as isogram
    if (check_isogram(string)):
        print("True")
    else:
        print("False")
 
    # checking str2 as isogram
    if (check_isogram(string2)):
        print("True")
 
    else:
        print("False")
 
# This code is contributed by AnkitRai01


C#




// C# code to check string is isogram or not
using System;
public class GFG {
 
    // function to check isogram
    static bool check_isogram(String str)
    {
 
        int length = str.Length;
        int[] mapHash = new int[26];
 
        // loop to store count of chars and
        // check if it is greater than 1
        for (int i = 0; i < length; i++) {
            mapHash[str[i] - 'a']++;
 
            // if count > 1, return false
            if (mapHash[str[i] - 'a'] > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String str = "geeks";
        String str2 = "computer";
 
        // checking str as isogram
        if (check_isogram(str))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
 
        // checking str2 as isogram
        if (check_isogram(str2))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript




<script>
    // Javascript code to check string is isogram or not
     
    // function to check isogram
    function check_isogram(str)
    {
 
        let length = str.length;
        let mapHash = new Array(26);
        mapHash.fill(0);
 
        // loop to store count of chars and
        // check if it is greater than 1
        for (let i = 0; i < length; i++)
        {
            mapHash[str[i].charCodeAt() - 'a'.charCodeAt()]++;
 
            // if count > 1, return false
            if (mapHash[str[i].charCodeAt() - 'a'.charCodeAt()] > 1)
            {
                return false;
            }
        }
 
        return true;
    }
     
    let str = "geeks";
    let str2 = "computer";
    
    // checking str as isogram
    if (check_isogram(str))
        document.write("True" + "</br>");
    else
        document.write("False" + "</br>");
    
    // checking str2 as isogram
    if (check_isogram(str2))
          document.write("True" + "</br>");
    else
        document.write("False" + "</br>");
     
    // This code is contributed by divyeshrabadiya07.
</script>


Output

False
True

Time Complexity: O(N)
Auxiliary Space: O(1), as an array of fixed size 26 is used.

Thanks Sahil Bansal for suggesting the above method. 
If you like w3wiki (We know you do!) and would like to contribute, you can also write an article using write.w3wiki.org or mail your article to review-team@w3wiki.org.



Check if a string is Isogram or not

Given a word or phrase, check if it is an isogram or not. An Isogram is a word in which no letter occurs more than once

Examples: 

Input: Machine
Output: True
Explanation: “Machine” does not have any character repeating, it is an Isogram

Input : Geek
Output : False
Explanation: “Geek” has ‘e’ as repeating character, it is not an Isogram

Similar Reads

Check if a string is Isogram or not using sorting:

To solve the problem follow the below idea:...

Check if a string is Isogram or not using Hash-Map:

...

Contact Us