-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'Find First Palindromic String in the Array' challenge
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
LeetCode/LeetCode/Challenges/FindFirstPalindromicStringInTheArray.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// FindFirstPalindromicStringInTheArray.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 06/03/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/find-first-palindromic-string-in-the-array/ | ||
struct FindFirstPalindromicStringInTheArray { | ||
|
||
//Time: O(n * m) where n is the number of elements in `words` | ||
// where m is the largest element in `words` | ||
//Space: O(1) | ||
//array | ||
//two pointers | ||
// | ||
//Solution Description: | ||
//Taking each element in `words`, we use two pointers starting at either end of `word`. We compare the element at each pointer, | ||
//if they match we increment and decrement to move onto the next elements and repeat the process; if they don't matchwe know | ||
//that this `word` is not a palindrome and can stop comparing elements. We reapt this process with each `word` until we find a | ||
//palindrome, which we immediately return. If after iterating through all elements in `words` we don't find a palindrome we | ||
//return an empty string. | ||
func firstPalindrome(_ words: [String]) -> String { | ||
for word in words { | ||
let characters = Array(word) | ||
|
||
var left = 0 | ||
var right = characters.count - 1 | ||
|
||
var isPalindrome = true | ||
|
||
while left < right { | ||
if characters[left] != characters[right] { | ||
isPalindrome = false | ||
break | ||
} | ||
|
||
left += 1 | ||
right -= 1 | ||
} | ||
|
||
if isPalindrome { | ||
return word | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
LeetCode/LeetCodeTests/Tests/FindFirstPalindromicStringInTheArrayTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// FindFirstPalindromicStringInTheArrayTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 06/03/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class FindFirstPalindromicStringInTheArrayTests: XCTestCase { | ||
|
||
// MARK: - Tests | ||
|
||
func test_A() { | ||
let words = ["abc","car","ada","racecar","cool"] | ||
|
||
let result = FindFirstPalindromicStringInTheArray().firstPalindrome(words) | ||
|
||
XCTAssertEqual(result, "ada") | ||
} | ||
|
||
func test_B() { | ||
let words = ["notapalindrome","racecar"] | ||
|
||
let result = FindFirstPalindromicStringInTheArray().firstPalindrome(words) | ||
|
||
XCTAssertEqual(result, "racecar") | ||
} | ||
|
||
func test_C() { | ||
let words = ["def","ghi"] | ||
|
||
let result = FindFirstPalindromicStringInTheArray().firstPalindrome(words) | ||
|
||
XCTAssertEqual(result, "") | ||
} | ||
} |