惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
GbyAI
GbyAI
M
MIT News - Artificial intelligence
美团技术团队
罗磊的独立博客
雷峰网
雷峰网
量子位
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
D
Docker
小众软件
小众软件
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
WordPress大学
WordPress大学
V
V2EX
博客园_首页
腾讯CDC
The Cloudflare Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
LeetCode 387: First Unique Character in a String — Simple...
Jerin · 2026-05-06 · via DEV Community

Jerin

Difficulty: Easy

Topics: Hash Table, String, Queue, Counting

Platform: Leetcode

Problem Statement

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Problem Statement Simplified
Get the index of the first non-repeating character, else return -1.

Mistakes and Learning
Do not blindly loop thorugh.
Do not just delete the duplicates, remove the duplicated character also.

Example 1

`Input: s = "leetcode"

Output: 0

Explanation:

The character 'l' at index 0 is the first character that does not occur at any other index.`

Example 2

`Input: s = "loveleetcode"

Output: 2`

Key Insight

  • Remove all the duplicating characters

  • Then get the first character

  • Get the first character index.

  • If no unique then -1.

Algorithm

  1. Convert the string to a char array.
  2. Intitialize a HashMap to store the character and the repeating counts.
  3. Intitialize a for loop
  4. Put the character in the HashMap with getOrDefault to check if the character already preset or not, if yes then increment by 1.
  5. End for loop
  6. Intitialize a for loop
  7. Check if the first value is 1 if yes then return the for loop index else continue the for loop
  8. End for loop
  9. return -1

Algorithm in simple words

First, convert the string to a char array. Then initialize a HashMap so that we can store each character with its repeating counts.
Then initialize a for loop to add each character from character array to HashMap with getOrDefault ( if the value of preseent the increment b 1 else it will be 0 ), end of for loop. Then initialize another for loop to check the first character with value 1 from HashMap. if found then return the loop index else it will end the loop and return -1.

Java code

class Solution {
    public int firstUniqChar(String s) {
        char[] character = s.toCharArray();
        Map <Character, Integer> map= new HashMap<>();
        for(int i=0;i<character.length;i++){
            map.put(character[i],map.getOrDefault(character[i],0)+1);
        }
       for(int i=0;i<character.length;i++){
        if(map.get(character[i])==1){
            return i;
        }
       }
        return -1;
    }
}

Enter fullscreen mode Exit fullscreen mode

Time & Space Complexity

Time Complexity: O(n)

Space Complexity: O(n)