题目描述:
1961. 检查字符串是否为数组前缀 - 力扣(LeetCode) (leetcode-cn.com)
自测用例:
"iloveleetcode"
["i","love","leetcode","apples"]
"iloveleetcode"
["apples","i","love","leetcode"]
"iloveleetcode"
["i","love","leetcodecn","apples"]
"iloveleetcode"
["i","love"]
"iloveleetcode"
["i","love","leetcode"]
"iloveleetcode"
["ix","love","leetcode","apples"]
Java代码:
class Solution {
public boolean isPrefixString(String s, String[] ws) {
int si=s.length();
for(int wi=0;si>0&&wi<ws.length;si-=ws[wi++].length());
if(si!=0)return false;
for(int wi=0;si!=s.length();wi++){
for(char c:ws[wi].toCharArray())if(s.charAt(si++)!=c)return false;
}
return true;
}
}
?
|