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 may assume the string contain only lowercase letters.

package leetcode.easy;

public class FirstUniqueCharacterInAString {
	public int firstUniqChar(String s) {
		java.util.HashMap<Character, Integer> count = new java.util.HashMap<Character, Integer>();
		int n = s.length();
		// build hash map : character and how often it appears
		for (int i = 0; i < n; i++) {
			char c = s.charAt(i);
			count.put(c, count.getOrDefault(c, 0) + 1);
		}

		// find the index
		for (int i = 0; i < n; i++) {
			if (count.get(s.charAt(i)) == 1)
				return i;
		}
		return -1;
	}

	@org.junit.Test
	public void test() {
		System.out.println(firstUniqChar("leetcode"));
		System.out.println(firstUniqChar("loveleetcode"));
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11875310.html

时间: 2024-08-25 03:55:19

LeetCode_387. First Unique Character in a String的相关文章

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

题目 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

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

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] 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

387. First Unique Character in a String

https://leetcode.com/problems/first-unique-character-in-a-string/#/description 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 = "lovel

Java [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. 解题思路: 开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,

[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