Determining if a String Has All Unique Characters

This problem is about determining if a given string has all unique characters. An example input would be "abcdefg" and the corresponding output would be "True".


Implement an algorithm to determine if a string has all unique characters.
Example input: "abcdefg"
Example output: True


This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string input = "abcdefg";

        static bool IsUnique(string input)
            //This solution is optimal because it uses a HashSet to store the characters.
            //HashSets are O(1) for adding and checking for existence.
            //This solution is O(n) because it has to iterate through the entire string.
            HashSet<char> set = new HashSet<char>();
            foreach (char c in input)
                if (set.Contains(c))
                    return false;
            return true;

A.I. Evaluation of the Solution

The candidate's solution is correct and uses a HashSet, which is an efficient data structure for this problem. The solution is also O(n), which is the best possible time complexity for this problem.

Evaluated at: 2022-11-29 08:15:32