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 }
  • 相关阅读:
    大型网站技术架构(七)网站的可扩展性架构
    【Spark深入学习 -15】Spark Streaming前奏-Kafka初体验
    结合案例深入解析模板方法设计模式
    android开发之自定义View 详解 资料整理 小冰原创整理,原创作品。
    1309:【例1.6】回文数(Noip1999)
    jQuery dataTables四种数据来源[转]
    CYQ.Data 轻量数据层之路 使用篇-MProc 存储过程与SQL 视频[最后一集] H (二十八)
    CRM系统项目总结
    同源策略:JSONP和CORS
    forms表单与modelfrom使用
  • 原文地址:https://www.cnblogs.com/lz87/p/7068006.html
Copyright © 2011-2022 走看看