leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母

leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母,第1张

leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母 leetcode刷题系列----模式1(Two Points 双指针)- 141:Longest Word in Dictionary through Deleting (Medium) 通过删除字母匹配字典里最长单词 Tips
  • 更多题解请见本系列目录
  • Java和C#核心代码完全一样, length() and Length, compareTo() and CompareTo(), for and foreach
  • 这题比较简单,遍历字符串数组后使用双指针比较目标字符s与字符串数组内的元素。
  • 在类内额外定义一个双指针的字符串是否属于子串的函数,代码逻辑结构会更清晰。
Python
class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        result = ''
        for item in dictionary:
            p_item = p_s = 0
            while p_itemlen(result) or (len(item)==len(result) and result>item):
                    result = item
        
        return result
C++
class Solution {
public:
    string findLongestWord(string s, vector& dictionary) {
        string longestWord = "";
        for(string target : dictionary)
        {
            int l1=longestWord.size(), l2=target.size();
            if(l1>l2 || (l1==l2 && longestWord.compare(target)<0)) continue;
            if(isSubstr(s, target)) longestWord = target;
        }
        return longestWord;
    }

private:
    bool isSubstr(string s, string target) {
        int i = 0, j = 0;
        while (i < s.length() && j < target.length()) {
            if (s[i] == target[j]) {
                j++;
            }
            i++;
        }
        return j == target.length();
    }

};
C#
public class Solution {
    public string FindLongestWord(string s, IList dictionary) {
        string longestWord = "";
        foreach(string target in dictionary)
        {
            int l1=longestWord.Length, l2=target.Length;
            if(l1>l2 || (l1==l2 && longestWord.CompareTo(target)<0)) continue;
            if(isSubstr(s, target)) longestWord = target;
        }
        return longestWord;
    }
    

private bool isSubstr(string s, string target) {
        int i = 0, j = 0;
        while (i < s.Length && j < target.Length) {
            if (s[i] == target[j]) {
                j++;
            }
            i++;
        }
        return j == target.Length;
    }
}
Java
class Solution {
    public String findLongestWord(String s, List d) {
        String longestWord = "";
        for (String target : d) {
            int l1 = longestWord.length(), l2 = target.length();
            if (l1 > l2 || (l1 == l2 && longestWord.compareTo(target) < 0)) {
                continue;
            }
            if (isSubstr(s, target)) {
                longestWord = target;
            }
        }
        return longestWord;
    }

    private boolean isSubstr(String s, String target) {
        int i = 0, j = 0;
        while (i < s.length() && j < target.length()) {
            if (s.charAt(i) == target.charAt(j)) {
                j++;
            }
            i++;
        }
        return j == target.length();
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5677130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存