列表

详情


1961. 检查字符串是否为数组前缀

给你一个字符串 s 和一个字符串数组 words ,请你判断 s 是否为 words前缀字符串

字符串 s 要成为 words前缀字符串 ,需要满足:s 可以由 words 中的前 kk正数 )个字符串按顺序相连得到,且 k 不超过 words.length

如果 swords前缀字符串 ,返回 true ;否则,返回 false

 

示例 1:

输入:s = "iloveleetcode", words = ["i","love","leetcode","apples"]
输出:true
解释:
s 可以由 "i"、"love" 和 "leetcode" 相连得到。

示例 2:

输入:s = "iloveleetcode", words = ["apples","i","love","leetcode"]
输出:false
解释:
数组的前缀相连无法得到 s 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: bool isPrefixString(string s, vector<string>& words) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 3.3 MB, 提交时间: 2021-08-16 10:26:44

func isPrefixString(s string, words []string) bool {
	temp := ""
    for _, word := range words {
        temp += word
        if temp == s {
            return true
        }
    }
    return false
	
}

上一题