首页 > 其他分享 >leetcode-1455-easy

leetcode-1455-easy

时间:2023-12-06 09:02:13浏览次数:35  
标签:word easy sentence int 1455 prefix searchWord return leetcode

Check If a Word Occurs As a Prefix of Any Word in a Sentence
Given a sentence that consists of some words separated by a single space, and a searchWord, check if searchWord is a prefix of any word in sentence.

Return the index of the word in sentence (1-indexed) where searchWord is a prefix of this word. If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.

A prefix of a string s is any leading contiguous substring of s.

Example 1:

Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
Example 2:

Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
Example 3:

Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.
Constraints:

1 <= sentence.length <= 100
1 <= searchWord.length <= 10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.

思路一:分割字符串后遍历

    public int isPrefixOfWord(String sentence, String searchWord) {
        String[] split = sentence.split("\\s");

        for (int i = 0; i < split.length; i++) {
            if (split[i].startsWith(searchWord)) {
                return i + 1;
            }
        }

        return -1;
    }

思路二:直接在字符串上进行比对

    public int isPrefixOfWord(String sentence, String searchWord) {
        int idx = 0;

        sentence = " " + sentence;
        outer:
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) == ' ') {
                idx++;

                for (int j = 0; j < searchWord.length(); j++) {
                    if (sentence.charAt(i + j + 1) != searchWord.charAt(j)) {
                        continue outer;
                    }
                }

                return idx;
            }
        }

        return -1;
    }

标签:word,easy,sentence,int,1455,prefix,searchWord,return,leetcode
From: https://www.cnblogs.com/iyiluo/p/17878740.html

相关文章

  • leetcode-2180-easy
    CountIntegersWithEvenDigitSumGivenapositiveintegernum,returnthenumberofpositiveintegerslessthanorequaltonumwhosedigitsumsareeven.Thedigitsumofapositiveintegeristhesumofallitsdigits.Example1:Input:num=4Ou......
  • leetcode-1572-easy
    MatrixDiagonalSumGivenasquarematrixmat,returnthesumofthematrixdiagonals.Onlyincludethesumofalltheelementsontheprimarydiagonalandalltheelementsonthesecondarydiagonalthatarenotpartoftheprimarydiagonal.Example1:......
  • leetcode-1550-easy
    ThreeConsecutiveOddsGivenanintegerarrayarr,returntrueiftherearethreeconsecutiveoddnumbersinthearray.Otherwise,returnfalse.Example1:Input:arr=[2,6,4,1]Output:falseExplanation:Therearenothreeconsecutiveodds.Example2:......
  • leetcode-1512-easy
    NumberofGoodPairsGivenanarrayofintegersnums,returnthenumberofgoodpairs.Apair(i,j)iscalledgoodifnums[i]==nums[j]andi<j.Example1:Input:nums=[1,2,3,1,1,3]Output:4Explanation:Thereare4goodpairs(0,3),(0,4),(......
  • CF1824B1 LuoTianyi and the Floating Islands (Easy Version) 题解
    题意:思路:由于$k∈[1,3]$,分类讨论:当$k=1$时,有人结点自身即为好结点,每种情况的期望为$\frac{1}{n}$,$n$种情况的期望和为$1$。最终答案即为$1$。当$k=2$时,$2$个有人结点之间的路径上的结点即为好结点,那么问题转化为:树上所有路径的结点......
  • C1. Good Subarrays (Easy Version)
    思路:我们枚举每一个左端点,对于每一个左端点,寻找最长的满足条件的区间,这个区间长度就是左端点对答案的贡献,可以发现具有单调性,右端点只会前进不会倒退。所以我们两个指针各扫一遍区间就可以。#include<bits/stdc++.h>#definelsp<<1#definersp<<1|1#definePIIpair<int,......
  • [LeetCode Hot 100] LeetCode19. 删除链表的倒数第N个结点
    题目描述思路一:采用两次遍历第一遍遍历先获取链表的长度length第二次从dummy节点开始走length-n步然后将该节点指向下下个节点思路二:采用一次遍历设置虚拟节点dummyHead指向head设定双指针p和q,初始都指向虚拟节点dummyHead移动q,直到p与q之间相隔的元素个数为n(即q走......
  • [LeetCode Hot 100] LeetCode21. 合并两个有序链表
    题目描述思路:新建dummy去"穿针引线"新建一个dummy节点去"穿针引线"注意最后返回的是dummy.next方法一:/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this......
  • 视频汇聚/音视频流媒体视频平台/视频监控EasyCVR分享页面无法播放,该如何解决?
    国标GB28181安防视频监控/视频集中存储/云存储EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安防视频监控的能力,也具备接入AI智能分析的......
  • 视频汇聚/音视频流媒体视频平台/视频监控EasyCVR分享页面无法播放,该如何解决?
    国标GB28181安防视频监控/视频集中存储/云存储EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安防视频监控的能力,也具备接入AI智能分析......