LeetCode 1961. 检查字符串是否为数组前缀
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 1961. 检查字符串是否为数组前缀
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
給你一個字符串 s 和一個字符串數組 words ,請你判斷 s 是否為 words 的 前綴字符串 。
字符串 s 要成為 words 的 前綴字符串 ,需要滿足:s 可以由 words 中的前 k(k 為 正數 )個字符串按順序相連得到,且 k 不超過 words.length 。
如果 s 是 words 的 前綴字符串 ,返回 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 僅由小寫英文字母組成來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/check-if-string-is-a-prefix-of-array
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
class Solution { public:bool isPrefixString(string s, vector<string>& words) {int n = s.size(), ct = 0;string t;for(int i = 0; i < words.size(); ++i){ct += words[i].size();t += words[i];if(ct > n) return false;else if(ct == n)break;}if(ct < n) return false;return s==t;} };8 ms 13.5 MB C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode 1961. 检查字符串是否为数组前缀的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 2002. 两个回文子
- 下一篇: LeetCode MySQL 1581.