gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Please solve with C#. Given a string s consists of lowercase letters, find and return…
- User
Please solve with C#. Given a string s consists of lowercase letters, find and return the first instance of a non-repeating character in it. If there is no such character, return '_'.
- Assistant
Here's a possible solution in C#:
using System; using System.Collections.Generic; public class Solution { public char FirstUniqChar(string s) { // Create a dictionary to store the frequency of each character Dictionary<char, int> charFrequency = new Dictionary<char, int>(); // Traverse the string and update the frequency of each character foreach(char c in s) { if(charFrequency.ContainsKey(c)) { charFrequency[c]++; } else { charFrequency[c] = 1; } } // Traverse the string again and return the first character with frequency 1 foreach(char c in s) { if(charFrequency[c] == 1) { return c; } } // If no non-repeating character is found, return '_' return '_'; } } public class Program { public static void Main(string[] args) { // Test the solution with example inputs Solution solution = new Solution(); string input = "leetcode"; char result = solution.FirstUniqChar(input); Console.WriteLine("The first non-repeating character in '" + input + "' is: " + result); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.