1024programmer Java Palindrome | String_Algorithm 5956. Find the first palindrome string in the array (java/c/c++/python/go/rust)

Palindrome | String_Algorithm 5956. Find the first palindrome string in the array (java/c/c++/python/go/rust)

Preface: This article is organized by the editor of Programming Notes# and mainly introduces algorithm 5956. Find the first palindrome string in the array (java/c/c++/python/go/rust) related knowledge, I hope it will be of some reference value to you.


Article Directory

  • 5956. Find the first palindrome string in the array& #xff1a;
  • Sample 1:
  • Sample 2:
  • Sample 3:
  • Tip :
  • Analysis
  • Problem solution
    • java
    • c
    • c& #43;+
    • python
    • go
    • rust

  • Original Question portal:https://leetcode-cn.com/problems/find-first-palindromic-string-in-the-array/

5956. Find the first palindrome string in the array:

You are given a string arraywords ,Find and return the first palindrome string in the array The first palindrome string. If there is no string that meets the requirements, return an empty string "".

Palindrome string is defined as :If a string is read forward and backwards the same, then the string is a Palindrome string.

Sample 1:

Input:
words = ["abc","car","ada","racecar"," cool"]

Output:
"ada"

Explanation:
The first palindrome string is "ada".
Note that "racecar" is also a palindrome string but it is not the first one.

Example 2:

Input:
words = ["notapalindrome","racecar"]

Output:
"racecar"

Explanation:
The first and only palindrome string is "racecar".

Example 3:

Input:
words = ["def","ghi"]

Output:
""

Explanation:
There is no palindrome string,so an empty string is returned.

Prompt:

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


Analysis

  • Faced with this algorithm question, I fell into deep thought.
  • It feels like traversing the array , and then traversing the characters , to determine whether it is a palindrome. There is nothing that can be greatly optimized.
  • The head and tail double pointers,determine whether the characters are equal,the pointers gradually overlap toward the middle.


Problem solution

java

class Solution
public String firstPalindrome(String[] words)
for (String word : words)
int l = 0;
int r = word.length() - 1;
while (l < r
&& word.charAt (l) == word.charAt(r))
l&# 43;+;
r--;

if ( l >= r)
return word;


return "";



c

char * firstPalindrome(char ** words , int wordsSize)
for (int i = 0; i < wordsSize; ++i)
char* word = words[i];
int l = 0;
int r = strlen(word) - 1;
while (l < r
&& word[l] = = word[r])
l++;
r--;

if (l >= r)
return word;


return "";


c++

class Solution
public:
string firstPalindrome(vector<string>& words)
for (string& word: words)
int l = 0;
int r = word.size() - 1;
while (l < r
&& word[ l] == word[r])
l++;
r--;

if (l >= r)
return word;


return " ";

;


python

class Solution:
def firstPalindrome(self, words: List[str]) -> str:
return next((word for word in words if word == word[::-1]), '' )


go

func firstPalindrome(words []string) string
for _, word := range words
l := 0
r := len (word) - 1
for l < r && word[l] =&#61 ; word[r]
l++
r--

if l >= r
return word


return ""


rustkeyword”>while (l < r
&& word[l] =&# 61; word[r])
l++;
r;

if (l >= r)
return word;


return “”;

;


python

class Solution:
def firstPalindrome(self, words: List[str]) -> str:
return next ((word for word in words if word == word[::-1]), '')


go

func firstPalindrome(words []string) string
for _, word := range words
l := 0
r :&# 61; len(word) - 1
for l < r && word[l] == word[r ]
l++
r--

if l >= r
return word


return ""


rust

impl Solution
pub fn first_palindrome(words: Vec<String>) -> String
for word in words. iter()
let bs = word.as_bytes();
let mut l = 0;
let mut r = word.len() - 1;
while l < r && bs[l] == bs[r]
l +&# 61; 1;
r -&# 61; 1;

if l >= r
return word.clone();


"".to_string()




Original title portal:https:// leetcode-cn.com/problems/find-first-palindromic-string-in-the-array/


Thank you very much for reading this article~
Welcome【👍Like】【⭐Collect】【📝Comment】~
It’s not hard to give up,But it must be cool to persist~
I hope we can all make progress every day A little bit~
This article is originally written by the white hat of Erdangjia:https://le-yi.blog.csdn.net/ blog~


impl Solution
pub fn first_palindrome(words: Vec <String>) -> String
for word in words.iter()
let bs = word.as_bytes() ;
let mut l = 0;
let mut r = word .len() - 1;
while l < r && bs[l] == bs[r]
l +&#61 ; 1;
r -&#61 ; 1;

if l >= r
return word.clone();


"".to_string()




Original title portal:https://leetcode -cn.com/problems/find-first-palindromic-string-in-the-array/


Thank you very much for reading this article~
Welcome【👍Like】【⭐Collect】【📝Comment】~
It’s not hard to give up,But it must be cool to persist~
I hope we can all make progress every day Diandian~
This article is originally written by the white hat of Erdangjia:https://le-yi.blog.csdn.net/ blog~


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/724839

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索