加载中...
1961-检查字符串是否为数组前缀(Check If String Is a Prefix of Array)
发表于:2021-12-03 | 分类: 简单
字数统计: 589 | 阅读时长: 2分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/check-if-string-is-a-prefix-of-array

英文原文

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

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.

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

 

Example 1:

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

Example 2:

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

 

Constraints:

  • 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 中的前 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 。

 

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • 1 <= s.length <= 1000
  • words[i]s 仅由小写英文字母组成

通过代码

高赞题解

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

​ 第一题,题目要咱看看,一个字符串s是否可以由字符串数组words的前几个字符串拼接而来。

​ 那我们直接拼接words的前几个字符串一直拼接到和s一样或者更长,再看两边是否相等

​ 第一题咱都懂,别想太多别犹豫。

class Solution {
public:
    bool isPrefixString(string s, vector<string>& words) {
        string tmp = "";
        int i = 0;
        while(tmp.size() < s.size() && i < words.size()) tmp += words[i++];  //拼接字符串
        return tmp == s;
    }
};

统计信息

通过次数 提交次数 AC比率
7465 13970 53.4%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1977-划分数字的方案数(Number of Ways to Separate Numbers)
下一篇:
1962-移除石子使总数最小(Remove Stones to Minimize the Total)
本文目录
本文目录