当前位置:网站首页>Leetcode 1961. Check whether the string is an array prefix

Leetcode 1961. Check whether the string is an array prefix

2022-06-11 10:31:00 I'm not xiaohaiwa~~~~

 Insert picture description here
Give you a string s And an array of strings words , Please judge s Is it words Of Prefix string .

character string s To be words Of Prefix string , Need to meet :s Can be words In front of k(k by Positive numbers ) Strings are concatenated in order to get , And k No more than words.length .

If s yes words Of Prefix string , return true ; otherwise , return false .

Example 1:

 Input :s = "iloveleetcode", words = ["i","love","leetcode","apples"]
 Output :true
 explain :
s  Can be  "i""love"  and  "leetcode"  Connected to get .

Example 2:

 Input :s = "iloveleetcode", words = ["apples","i","love","leetcode"]
 Output :false
 explain :
 Array prefixes cannot be concatenated to get  s .
 

Tips :

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • 1 <= s.length <= 1000
  • words[i] and s It's only made up of lowercase letters

Code:

class Solution {
    
public:
    bool isPrefixString(string s, vector<string>& words) {
    
        int size=words.size();
    
        string res;
        for(int i=0;i<size;i++)
        {
    
            res+=words[i];
            
            if(res.length()>=s.length())
            {
    
                if(res==s)
                    return true;
            }
        }
        return false;
    }
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111028393811.html