Add to List 392. Is Subsequence

本文介绍了一种算法,用于判断一个字符串s是否为另一个字符串t的子序列。通过遍历两个字符串并比较字符来实现,适用于长度不同的字符串,例如s长度不超过100,而t可能长达500,000。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).

Example 1:
s = “abc”, t = “ahbgdc”

Return true.

Example 2:
s = “axc”, t = “ahbgdc”

Return false.

解答:
从s和t的首端开始遍历比较字符是否相等,如果不等,则增加在t中的下标位置;
如果相等,则同时增加在s和t中的下标位置。
如果t中的指标位置增长到了t的末尾,而s中的指标还没有增长的末尾,则返回false。如果s中的指标先增长到了末尾,则返回true。

C语言:

bool isSubsequence(char* s, char* t) {
    while(*t) {
        s += (*s == *t++);
    }   
    return !*s;
}

C++:

class Solution {
public:
    bool isSubsequence(string s, string t) {
        if(s == "" && t == "") return true;
        for(int i = 0, j = 0; j < t.size(); j++) {
            if(t[j] == s[i]) i++;
            if(i == s.size()) return true;
        }
        return false;
    }
};
Group az webapp : Manage web apps. Subgroups: auth : Manage webapp authentication and authorization. To use v2 auth commands, run "az extension add --name authV2" to add the authV2 CLI extension. config : Configure a web app. connection : Commands to manage webapp connections. cors : Manage Cross-Origin Resource Sharing (CORS). deleted [Preview] : Manage deleted web apps. deployment : Manage web app deployments. hybrid-connection : Methods that list, add and remove hybrid-connections from webapps. identity : Manage web app's managed identity. log : Manage web app logs. sitecontainers : Manage linux web apps sitecontainers. traffic-routing : Manage traffic routing for web apps. vnet-integration : Methods that list, add, and remove virtual network integrations from a webapp. webjob : Allows management operations for webjobs on a web app. Commands: browse : Open a web app in a browser. This is not supported in Azure Cloud Shell. create : Create a web app. create-remote-connection : Creates a remote connection using a tcp tunnel to your web app. delete : Delete a web app. deploy : Deploys a provided artifact to Azure Web Apps. list : List web apps. list-instances : List all scaled out instances of a web app or web app slot. list-runtimes : List available built-in stacks which can be used for web apps. restart : Restart a web app. show : Get the details of a web app. ssh [Preview] : SSH command establishes a ssh session to the web container and developer would get a shell terminal remotely. start : Start a web app. stop : Stop a web app. up : Create a webapp and deploy code from a local workspace to the app. The command is required to run from the folder where the code is present. Current support includes Node, Python, .NET Core and ASP.NET. Node, Python apps are created as Linux apps. .Net Core, ASP.NET, and static HTML apps are created as Windows apps. Append the html flag to deploy as a static HTML app. Each time the command is successfully run, default argument values for resource group, sku, location, plan, and name are saved for the current directory. These defaults are then used for any arguments not provided on subsequent runs of the command in the same directory. Use 'az configure' to manage defaults. Run this command with the --debug parameter to see the API calls and parameters values being used. update : Update an existing web app. To search AI knowledge base for examples, use: az find "az webapp" You have 2 update(s) available. They will be updated with the next build of Cloud Shell.
最新发布
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值