zoukankan      html  css  js  c++  java
  • [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.

    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 }
  • 相关阅读:
    转:Jmeter之Bean shell使用(一)
    转:Curl详解
    转:MIME(Multipurpose Internet Mail Extensions)类型
    转:Tomcat配置
    转:windows 7系统安装与配置Tomcat服务器环境
    转:Apache POI Tutorial
    转:Busy Developers' Guide to HSSF and XSSF Features
    转:Java实现几种常见排序方法
    转:JAVA强制类型转换
    转:Java的各种类型转换汇总
  • 原文地址:https://www.cnblogs.com/lz87/p/7068006.html
Copyright © 2011-2022 走看看