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

题目:
给你一个字符串 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 。

        public static bool IsPrefixString(string s, string[] words)
        {
            string a = "";
            int lg = s.Length;
            for (int i = 0; i < words.Length; i++)
            {
                a += words[i];
                if (a==s)
                {
                    return true;
                }
                else if (a.Length > lg)
                {
                    return false;
                }
            }
            return false;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值