leetcode 1961. Check If String Is a Prefix of Array(python)

这是我参与8月更文挑战的第12天,活动详情查看:8月更文挑战markdown

描述

Given a string s and an array of strings words, determine whether s is a prefix string of words.app

A string s is a prefix string of words if s can be made by concatenating the first k strings in words for some positive k no larger than words.length.less

Return true if s is a prefix string of words, or false otherwise.oop

Example 1:post

Input: s = "iloveleetcode", words = ["i","love","leetcode","apples"]
Output: true
Explanation:
s can be made by concatenating "i", "love", and "leetcode" together.
复制代码

Example 2:spa

Input: s = "iloveleetcode", words = ["apples","i","love","leetcode"]
Output: false
Explanation:
It is impossible to make s using a prefix of arr.
复制代码

Note:code

1 <= words.length <= 100
1 <= words[i].length <= 20
1 <= s.length <= 1000
words[i] and s consist of only lowercase English letters.
复制代码

解析

根据题意,给出了一个字符串 s ,还有一个单词列表 words ,让咱们判断 s 是不是 words 的前缀字符串。题目中给出了定义判断字符串 s 是不是列表 words 的前缀,只须要判断 s 是否和 words 中的前 k 个字符串拼接起来的字符串相同便可,k 要小于 words 的长度。orm

其实思路已经出来了:leetcode

  • 初始化一个须要不断拼接的字符串 prefix
  • 遍历 words 中的单词,将当前的单词 word 拼接到 prefix 后面,判断此时的 prefix 若是和 s 相等,直接返回 True ,不然重复此过程
  • 遍历结束若是没有返回 True ,则表示没有找到 prefix ,直接返回 False

解答

class Solution(object):
    def isPrefixString(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: bool
        """
        prefix = ''
        for word in words:
            prefix += word
            if s==prefix:
                return True
        return False
        	      
		
复制代码

运行结果

Runtime: 20 ms, faster than 93.69% of Python online submissions for Check If String Is a Prefix of Array.
Memory Usage: 13.4 MB, less than 71.17% of Python online submissions for Check If String Is a Prefix of Array.
复制代码

解析

另外,咱们能够直接将 words 中的单词拼接成一个字符串 r ,而后判断 s 是不是 r 的前缀。可是要保证不能是“伪前缀”,如 s=a ,words=["aa","aaaa","banana"] ,这种输入应该是 False ,因此还要保证 s 的长度必须是合理的。原理同样,换汤不换药。字符串

解答

class Solution(object):
    def isPrefixString(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: bool
        """
        lengths = [len(words[0])]
        for word in words[1:]:
            lengths.append(len(word)+lengths[-1])
        return len(s) in lengths and ''.join(words).startswith(s)           	      
		
复制代码

运行结果

Runtime: 20 ms, faster than 93.69% of Python online submissions for Check If String Is a Prefix of Array.
Memory Usage: 13.6 MB, less than 22.97% of Python online submissions for Check If String Is a Prefix of Array.
复制代码

原题连接:leetcode.com/problems/ch…

您的支持是我最大的动力

相关文章
相关标签/搜索