zoukankan      html  css  js  c++  java
  • LF.395.Remove Certain Characters

    Remove given characters in input string, the relative order of other characters should be remained. Return the new string after deletion.

    Assumptions

    The given input string is not null.
    The characters to be removed is given by another string, it is guranteed to be not null.
    Examples

    input = "abcd", t = "ab", delete all instances of 'a' and 'b', the result is "cd".


    time: o(n) space o(n)
    public String remove(String input, String t) {
            // Write your solution here
            char[] inputs = input.toCharArray() ;
            char[] ts = t.toCharArray() ;
            //put all the ts input hashset
            Set<Character> set = new HashSet<>(ts.length) ;
            StringBuilder sb = new StringBuilder() ;
            for (int i = 0; i < ts.length; i++) {
                set.add(ts[i]);
            }
            for (int i = 0; i <inputs.length ; i++) {
                if (!set.contains(inputs[i])){
                    sb.append(inputs[i]);
                }
            }
            return sb.toString();
        }
        //another method using two pointers: valid area: [0, slow)
        public String remove_twopointer(String input, String t) {
            // Write your solution here
            int slow = 0, fast = 0 ;
            //put the t into hashset
            Set<Character> set = new HashSet<>(t.length());
            char[] inputs = input.toCharArray() ;
            for (int k = 0; k < t.length(); k++) {
                set.add(t.charAt(k));
            }
            //only need to check the fast
            /*          d
            *       s t u d
            *       i i i-i
            *       j j j j j
    String
    public String(char[] value,
                  int offset,
                  int count)
    Allocates a new String that contains characters from a subarray of the character array argument.
    The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray.
    The contents of the subarray are copied;
    subsequent modification of the character array does not affect the newly created string.
    
    Parameters:
    value - Array that is the source of characters
    offset - The initial offset
    count - The length
    
            * */
            while (fast<input.length()){
                if (set.contains(input.charAt(fast))){
                    fast++ ;
                } else{
                    inputs[slow++] = inputs[fast++] ;
                }
            }
            return new String(inputs, 0, slow);
        }
  • 相关阅读:
    定时任务 cron 学习
    [转]交换机互联不同 vlan 及不同网段通信问题总结
    H3C Cloud Lab配置不生效的解决办法
    使用NPTP+SecureCRT连接H3C Cloud Lab中的路由器
    postgresql 创建索引
    php多进程应用场景实例详解
    pcntl扩展学习
    Kubernetes进阶实战读书笔记:Daemonset控制器|Job控制器
    Kubernetes进阶实战读书笔记:资源需求及限制
    Kubernetes进阶实战读书笔记:POD对象的生命周期(探针检测)
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8721195.html
Copyright © 2011-2022 走看看