[LintCode] First Position Unique Character
Posted Push your limit!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LintCode] First Position Unique Character相关的知识,希望对你有一定的参考价值。
Given a string, find the first non-repeating character in it and return it‘s index. If it doesn‘t exist, return -1
.
Given s = "lintcode"
, return 0
.
Given s = "lovelintcode"
, return 2
.
Solution 1. Two steps‘ iteration
1. first time iterate through s and create a hash map that maps each unique character to the set of its appearance indices in s.
2. second time iterate through s and return the index of the first character whose apperance set size is 1. return -1 if no such character is found.
1 public class Solution { 2 public int firstUniqChar(String s) { 3 if(s == null || s.length() == 0){ 4 return -1; 5 } 6 HashMap<Character, HashSet<Integer>> map = new HashMap<Character, HashSet<Integer>>(); 7 for(int i = 0; i < s.length(); i++){ 8 if(map.containsKey(s.charAt(i))){ 9 map.get(s.charAt(i)).add(i); 10 } 11 else{ 12 HashSet<Integer> set = new HashSet<Integer>(); 13 set.add(i); 14 map.put(s.charAt(i),set); 15 } 16 } 17 for(int i = 0; i < s.length(); i++){ 18 if(map.get(s.charAt(i)).size() == 1){ 19 return i; 20 } 21 } 22 return -1; 23 } 24 }
Optimization on solution 1
Solution 1 already achieves the BCR since we have to iterate through s at least once.
But we can still optimize on memory usage. In solution 1, no matter what kind of input
we get, we always use O(n) space because the hash map stores all characters‘ indices.
Instead of keeping an appearance index set for each unique character, we can simply keep
an integer that counts the appearance time of each unique character. Doing this saves
memory usage on average cases. In the worst case that each character is unique, we still will
use O(n) memory.
1 public class Solution { 2 public int firstUniqChar(String s) { 3 if(s == null || s.length() == 0){ 4 return -1; 5 } 6 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 7 for(int i = 0; i < s.length(); i++){ 8 if(map.containsKey(s.charAt(i))){ 9 map.put(s.charAt(i), map.get(s.charAt(i)) + 1); 10 } 11 else{ 12 map.put(s.charAt(i),1); 13 } 14 } 15 for(int i = 0; i < s.length(); i++){ 16 if(map.get(s.charAt(i)) == 1){ 17 return i; 18 } 19 } 20 return -1; 21 } 22 }
If we can make an assumption about the input string‘s character set is the ASCII set, then we can reduce
memory usage to O(1).
1 public class Solution { 2 public int firstUniqChar(String s) { 3 int[] alp = new int[256]; 4 char[] arr =s.toCharArray(); 5 for(char c : arr ){ 6 alp[c]++; 7 } 8 for(int i = 0; i < arr.length; i++){ 9 if (alp[arr[i]]==1) return i; 10 } 11 return -1; 12 } 13 }
以上是关于[LintCode] First Position Unique Character的主要内容,如果未能解决你的问题,请参考以下文章
Lintcode14 First Position of Target Solution 题解
lintcode14/458 First/Last Position of Target
[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Arr
第一个独特字符位置 · first position unique character
[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version