Skip to content

Commit b1b348c

Browse files
solvesfind first palindrome string in array
1 parent 72ca041 commit b1b348c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,4 @@
499499
| 2094 | [Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers) | [![Java](assets/java.png)](src/Finding3DigitEvenNumbers.java) | |
500500
| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum) | [![Java](assets/java.png)](src/FindSubsequenceOfLengthKWithTheLargestSum.java) | |
501501
| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods) | [![Java](assets/java.png)](src/RingsAndRods.java) | |
502+
| 2108 | [Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array) | [![Java](assets/java.png)](src/FindFirstPalindromicStringInArray.java) | |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/find-first-palindromic-string-in-the-array
2+
// T: O(|words| * len(words[i]))
3+
// S: O(1)
4+
5+
public class FindFirstPalindromicStringInArray {
6+
public String firstPalindrome(String[] words) {
7+
for (String word : words) {
8+
if (isPalindrome(word)) {
9+
return word;
10+
}
11+
}
12+
return "";
13+
}
14+
15+
private boolean isPalindrome(String s) {
16+
for (int i = 0; i < s.length() / 2 ; i++) {
17+
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)