返回介绍

solution / 2100-2199 / 2108.Find First Palindromic String in the Array / README_EN

发布于 2024-06-17 01:03:09 字数 4604 浏览 0 评论 0 收藏 0

2108. Find First Palindromic String in the Array

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def firstPalindrome(self, words: List[str]) -> str:
    return next((w for w in words if w == w[::-1]), "")
class Solution {
  public String firstPalindrome(String[] words) {
    for (var w : words) {
      boolean ok = true;
      for (int i = 0, j = w.length() - 1; i < j && ok; ++i, --j) {
        if (w.charAt(i) != w.charAt(j)) {
          ok = false;
        }
      }
      if (ok) {
        return w;
      }
    }
    return "";
  }
}
class Solution {
public:
  string firstPalindrome(vector<string>& words) {
    for (auto& w : words) {
      bool ok = true;
      for (int i = 0, j = w.size() - 1; i < j; ++i, --j) {
        if (w[i] != w[j]) {
          ok = false;
        }
      }
      if (ok) {
        return w;
      }
    }
    return "";
  }
};
func firstPalindrome(words []string) string {
  for _, w := range words {
    ok := true
    for i, j := 0, len(w)-1; i < j && ok; i, j = i+1, j-1 {
      if w[i] != w[j] {
        ok = false
      }
    }
    if ok {
      return w
    }
  }
  return ""
}
function firstPalindrome(words: string[]): string {
  for (const word of words) {
    let left = 0;
    let right = word.length - 1;
    while (left < right) {
      if (word[left] !== word[right]) {
        break;
      }
      left++;
      right--;
    }
    if (left >= right) {
      return word;
    }
  }
  return '';
}
impl Solution {
  pub fn first_palindrome(words: Vec<String>) -> String {
    for word in words.iter() {
      let s = word.as_bytes();
      let mut left = 0;
      let mut right = s.len() - 1;
      while left < right {
        if s[left] != s[right] {
          break;
        }
        left += 1;
        right -= 1;
      }
      if left >= right {
        return word.clone();
      }
    }
    String::new()
  }
}
char* firstPalindrome(char** words, int wordsSize) {
  for (int i = 0; i < wordsSize; i++) {
    int left = 0;
    int right = strlen(words[i]) - 1;
    while (left < right) {
      if (words[i][left] != words[i][right]) {
        break;
      }
      left++;
      right--;
    }
    if (left >= right) {
      return words[i];
    }
  }
  return "";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文