zoukankan      html  css  js  c++  java
  • [LintCode] 194. Find Words

    Given a string str and a dictionary dict, you need to find out which words in the dictionary are subsequences of the string and return those words.The order of the words returned should be the same as the order in the dictionary.

    Example

    Example 1:

    Input:
    str="bcogtadsjofisdhklasdj"
    dict=["book","code","tag"]
    Output:
    ["book"]
    Explanation:Only book is a subsequence of str
    

    Example 2:

    Input:
    str="nmownhiterer"
    dict=["nowhere","monitor","moniter"]
    Output:
    ["nowhere","moniter"]

    Solution 1:
    public class Solution {
        /**
         * @param str: the string
         * @param dict: the dictionary
         * @return: return words which  are subsequences of the string
         */
        public List<String> findWords(String str, List<String> dict) {
            // Write your code here.
            List<String> res = new ArrayList<>();
            for (String item: dict) {
                int sItem = 0;
                int sStart = 0;
                while (sItem < item.length() && sStart < str.length()) {
                    if (item.charAt(sItem) == str.charAt(sStart)) {
                        sItem += 1;
                    }
                    if (sItem == item.length()) {
                        res.add(item);
                        break;
                    }
                    sStart += 1;
                }
            }
            return res;
        }
    }
  • 相关阅读:
    Java 插入排序
    Java 浮点型与双精度数值比较
    Java 包装类Integer的值比较
    ORA-00942 table or view does not exist
    logging模块
    面向对象
    模块和包
    异常处理
    序列化模块
    css3选择器
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12515161.html
Copyright © 2011-2022 走看看