码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] First Position Unique Character

时间:2017-06-23 10:17:03      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:ring   osi   log   hose   iter   res   sage   find   mat   

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

Example

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

标签:ring   osi   log   hose   iter   res   sage   find   mat   

原文地址:http://www.cnblogs.com/lz87/p/7068006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!