首页 > 其他分享 >[LeetCode] 2108. Find First Palindromic String in the Array

[LeetCode] 2108. Find First Palindromic String in the Array

时间:2024-02-13 14:11:56浏览次数:32  
标签:return string Palindromic palindromic words 字符串 Find String

Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".

A string is palindromic if it reads the same forward and backward.

Example 1:
Input: words = ["abc","car","ada","racecar","cool"]
Output: "ada"
Explanation: The first string that is palindromic is "ada".
Note that "racecar" is also palindromic, but it is not the first.

Example 2:
Input: words = ["notapalindrome","racecar"]
Output: "racecar"
Explanation: The first and only string that is palindromic is "racecar".

Example 3:
Input: words = ["def","ghi"]
Output: ""
Explanation: There are no palindromic strings, so the empty string is returned.

Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists only of lowercase English letters.

找出数组中的第一个回文字符串。

给你一个字符串数组 words ,找出并返回数组中的 第一个回文字符串 。如果不存在满足要求的字符串,返回一个 空字符串 "" 。
回文字符串 的定义为:如果一个字符串正着读和反着读一样,那么该字符串就是一个 回文字符串 。

思路

找到 input 数组里第一个出现的回文串。

复杂度

时间O(mn) - m个单词,单词平均长度为n
空间O(1)

代码

Java实现

class Solution {
    public String firstPalindrome(String[] words) {
        for (String w : words) {
            if (helper(w)) {
                return w;
            }
        }
        return "";
    }

    public boolean helper(String word) {
        int left = 0;
        int right = word.length() - 1;
        while (left < right) {
            if (word.charAt(left) != word.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

相关题目

7. Reverse Integer
9. Palindrome Number
2108. Find First Palindromic String in the Array

标签:return,string,Palindromic,palindromic,words,字符串,Find,String
From: https://www.cnblogs.com/cnoodle/p/18014575

相关文章

  • 字符串Stringjoiner
    不能添加数字,只能添加字符串......
  • 字符串StringBulider
    ......
  • 【C++】STL string类例题新思路记录(编写一个程序,告诉用户输入的句子包含多少个元音字
    题干:编写一个程序,告诉用户输入的句子包含多少个元音字母。 方案一:1、创建一个普通函数,依次传入5个元音字母对查找字符串进行检测。2、函数通过依次传入的单个元音字母,循环查找整个字符串最后返回统计值。1#include<string>2#include<iostream>3usingnamespace......
  • CF316G3 Good Substrings
    题意简述有一个字符串\(s\)和\(n\)条限制,每条限制给出字符串\(t_i\)和两个整数\(l_i,r_i\),要求一个字符串要满足在\(t_i\)中的出现次数要在\([l_i,r_i]\)之间。求\(s\)有多少本质不同的子串满足所有限制。\(|s|,\max|t|\le5\times10^4,n\le10\)分析“本质不同......
  • D. Find the Different Ones!
    前言拿到题目首先看数据量,n,q都是2e5的数量级,如果是暴力解的话时间复杂度会达到O(m*n)(最差情况m次询问,每次l和r为1和n),很明显会超时。这就意味着我们要在线性的时间内完成查询,即每次询问的查询时间保证在O(1)。题解准备一个数组b存储该连续相同数字串的起始点,然后我们从左向右遍历......
  • B. Following the String
    题解我们从左向右构建字符串。每种数字我们都从a开始取,接下来我们用一个数组来存储对应数字下一次要取的字母。Code #include<bits/stdc++.h>usingnamespacestd;constintN=2e5+5;intb[N],a[N];intmain(){intt;cin>>t;while(t--){strin......
  • Java中String、StringBuffer、StringBuilder的区别以及使用场景总结
    Java中,String、StringBuffer和StringBuilder都用于处理字符串,但在功能和性能上有显著的区别。了解这些区别有助于选择最适合特定情境的类型。在选择使用String、StringBuffer或StringBuilder时,应根据字符串操作的性能需求和线程安全要求来做出决定。1、String、StringBuffer、......
  • D. Find the Different Ones!
    原题链接核心\(p[i]\)代表离\(a[i]\)最近的不同元素code#include<bits/stdc++.h>usingnamespacestd;inta[200005]={0};intp[200005]={0};intmain(){intt;cin>>t;while(t--){intn;cin>>n;for(inti=1......
  • mysql插入数据出现java.sql.SQLException Create breakpoint : Incorrect string valu
    问题图片如下:  如果出现这个问题,就是当前的mysql设置的字符集和当前业务的需求符合;前:当前我需要在mysql中存入的内容中包括了表情等信息,如下:问题分析:因为我在docker中搭建的mysql设置的默认编码为utf-8,下面是我的my.cnf文件[client]default_character_set=utf8[my......
  • 153. Find Minimum in Rotated Sorted Array
    题目Supposeanarraysortedinascendingorderisrotatedatsomepivotunknowntoyoubeforehand.(i.e., [0,1,2,4,5,6,7] mightbecome [4,5,6,7,0,1,2]).Findtheminelement.Youmayassumenoduplicateexistsinthearray.Example1:Input:[3,4,5,1,2]O......