成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Missing String

IamDLY / 1631人閱讀

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;
    }
};

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70943.html

相關(guān)文章

  • [LintCode] First Missing Positive

    摘要:找第一個(gè)缺失的正整數(shù),只要先按順序排列好,也就是,找到第一個(gè)和不對(duì)應(yīng)的數(shù)就可以了。注意數(shù)組的從開始,而正整數(shù)從開始,所以重寫排列的時(shí)候要注意換成,而就是從開始的數(shù)組中的元素。 Problem Given an unsorted integer array, find the first missing positive integer. Example Given [1,2,0] re...

    snifes 評(píng)論0 收藏0
  • [LintCode] Find the Missing Number [三種方法]

    摘要:求和相減是先求出到這個(gè)等差數(shù)列的和,再減去實(shí)際數(shù)組的和,就是缺失的數(shù),第二種方法是,只要先按順序排列好,用二分法找到第一個(gè)和不相等的數(shù)就可以了。二分法求和相減法共個(gè)數(shù),多加了一個(gè)異或法 Problem Given an array contains N numbers of 0 .. N, find which number doesnt exist in the array. Exa...

    liaoyg8023 評(píng)論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 評(píng)論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 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<