From 73c77e96454a9a0ac9b3a794286187cbe4734cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:26:36 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=20387=20(First=20?= =?UTF-8?q?Unique=20Character=20in=20a=20String)=20from=20Swift=20to=20C++?= =?UTF-8?q?23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/387.swift with solutions/0301-0400/0387-first-unique-character-in-a-string/, using a fixed 26-slot count array instead of a hash map. --- .../README.md | 11 +++++++++ .../solution.cpp | 23 +++++++++++++++++++ .../solution.hpp | 11 +++++++++ .../test.cpp | 9 ++++++++ swift/387.swift | 17 -------------- 5 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 solutions/0301-0400/0387-first-unique-character-in-a-string/README.md create mode 100644 solutions/0301-0400/0387-first-unique-character-in-a-string/solution.cpp create mode 100644 solutions/0301-0400/0387-first-unique-character-in-a-string/solution.hpp create mode 100644 solutions/0301-0400/0387-first-unique-character-in-a-string/test.cpp delete mode 100644 swift/387.swift diff --git a/solutions/0301-0400/0387-first-unique-character-in-a-string/README.md b/solutions/0301-0400/0387-first-unique-character-in-a-string/README.md new file mode 100644 index 0000000..21ed255 --- /dev/null +++ b/solutions/0301-0400/0387-first-unique-character-in-a-string/README.md @@ -0,0 +1,11 @@ +# 387. First Unique Character in a String + +[LeetCode problem 387](https://leetcode.com/problems/first-unique-character-in-a-string/) + +- **Difficulty**: Easy +- **Tags**: hash-table, string, queue, counting + +## Description + +Given a string `s`, return the index of the first non-repeating character, +or `-1` if none exists. diff --git a/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.cpp b/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.cpp new file mode 100644 index 0000000..237198e --- /dev/null +++ b/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.cpp @@ -0,0 +1,23 @@ +#include "solution.hpp" + +#include + +namespace leetcode::p0387 { + +int solve(const std::string& s) { + std::array counts{}; + + for (char c : s) { + ++counts.at(static_cast(c - 'a')); + } + + for (std::size_t i = 0; i < s.size(); ++i) { + if (counts.at(static_cast(s[i] - 'a')) == 1) { + return static_cast(i); + } + } + + return -1; +} + +} // namespace leetcode::p0387 diff --git a/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.hpp b/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.hpp new file mode 100644 index 0000000..72d932c --- /dev/null +++ b/solutions/0301-0400/0387-first-unique-character-in-a-string/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p0387 { + +// optimized: O(n) time, O(1) space — fixed 26-slot count array (assumes +// lowercase English letters, per the problem's constraints), two passes. +int solve(const std::string& s); + +} // namespace leetcode::p0387 diff --git a/solutions/0301-0400/0387-first-unique-character-in-a-string/test.cpp b/solutions/0301-0400/0387-first-unique-character-in-a-string/test.cpp new file mode 100644 index 0000000..9c7096a --- /dev/null +++ b/solutions/0301-0400/0387-first-unique-character-in-a-string/test.cpp @@ -0,0 +1,9 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 387", "[p0387]") { + CHECK(leetcode::p0387::solve("leetcode") == 0); + CHECK(leetcode::p0387::solve("loveleetcode") == 2); + CHECK(leetcode::p0387::solve("aabb") == -1); +} diff --git a/swift/387.swift b/swift/387.swift deleted file mode 100644 index 3f4dece..0000000 --- a/swift/387.swift +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { - func firstUniqChar(_ s: String) -> Int { - var dict: [Character : Int] = [:] - - for character in s { - dict[character, default: 0] += 1 - } - - for (i, v) in s.enumerated() { - if dict[v] == 1 { - return i - } - } - - return -1 - } -} \ No newline at end of file