Leetcode: First Unique Character in a String

Given a string, find the first non-repeating character in it and return it‘s index. If it doesn‘t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
 1 public class Solution {
 2     public int firstUniqChar(String s) {
 3         int[] count = new int[26];
 4         for (int i=0; i<s.length(); i++) {
 5             count[s.charAt(i) - ‘a‘]++;
 6         }
 7         for (int i=0; i<s.length(); i++) {
 8             if (count[s.charAt(i) - ‘a‘] == 1)
 9                 return i;
10         }
11         return -1;
12     }
13 }
时间: 2024-08-03 17:56:21

Leetcode: First Unique Character in a String的相关文章

[LeetCode] First Unique Character in a String 字符串第一个不同字符

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

LeetCode(387)First Unique Character in a String

题目 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc

Leetcode-387 First Unique Character in a String

#387.   First Unique Character in a String Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

LeetCode_387. First Unique Character in a String

387. First Unique Character in a String Easy Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You

【LeetCode】387. First Unique Character in a String

Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s =

[Leetcode] DP-- 467. Unique Substrings in Wraparound String

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find

209 First Unique Character in a String

原题网址:https://www.lintcode.com/problem/first-unique-character-in-a-string/description 描述 给出一个字符串,找出第一个只出现一次的字符. 您在真实的面试中是否遇到过这个题?  是 样例 对于 "abaccdeff", 'b'为第一个只出现一次的字符. 标签 字符串处理 思路:用两个哈希表,一个统计每个字符出现次数,另一个统计字符的下标.遍历第一个哈希表,找到字符出现次数为1并且下标最小的return出去

leetcode修炼之路——387. First Unique Character in a String

最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: Y

[LeetCode] NO. 387 First Unique Character in a String

[题目] Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowe