资讯专栏INFORMATION COLUMN

[LintCode] Missing String

IamDLY / 1573人阅读

Problem

Given two strings, you have to find the missing string.

Example

Given a string str1 = This is an example
Given another string str2 = is example

Return ["This", "an"]

Solution
public class Solution {
    /*
     * @param : a given string
     * @param : another given string
     * @return: An array of missing string
     */
    public List missingString(String str1, String str2) {
        // Write your code here
        List res = new ArrayList<>();
        String[] s1 = str1.split(" ");
        String[] s2 = str2.split(" ");
        if (s1.length == s2.length) {
            return res;
        }
        //assume s1.length > s2.length
        if (s1.length < s2.length) {
            String[] temp = s1;
            s1 = s2;
            s2 = temp;
        }
        
        Set unique = new HashSet<>();
        
        //save the short string array in hashset
        for (String s: s2) {
            unique.add(s);
        }
        
        //check the long string array and put the missing strings in result
        for (String s: s1) {
            if (!unique.contains(s)) {
                res.add(s);
            }
        }
        
        return res;
    }
};

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70943.html

相关文章

  • [LintCode] First Missing Positive

    摘要:找第一个缺失的正整数,只要先按顺序排列好,也就是,找到第一个和不对应的数就可以了。注意数组的从开始,而正整数从开始,所以重写排列的时候要注意换成,而就是从开始的数组中的元素。 Problem Given an unsorted integer array, find the first missing positive integer. Example Given [1,2,0] re...

    snifes 评论0 收藏0
  • [LintCode] Find the Missing Number [三种方法]

    摘要:求和相减是先求出到这个等差数列的和,再减去实际数组的和,就是缺失的数,第二种方法是,只要先按顺序排列好,用二分法找到第一个和不相等的数就可以了。二分法求和相减法共个数,多加了一个异或法 Problem Given an array contains N numbers of 0 .. N, find which number doesnt exist in the array. Exa...

    liaoyg8023 评论0 收藏0
  • [LintCode/LeetCode] Set Mismatch

    Problem The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetit...

    Astrian 评论0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 评论0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 评论0 收藏0

发表评论

0条评论

IamDLY

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<