Created
April 7, 2021 11:27
-
-
Save vrat28/68afd6099f89b5606fba396d595126d8 to your computer and use it in GitHub Desktop.
Determine if two string halves are equal (Leetcode 1704)
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
class Solution { | |
func halvesAreAlike(_ s: String) -> Bool { | |
var count = 0 | |
var low = 0 , high = s.count - 1 | |
// For the ease of traversal | |
let array = Array(s) | |
while low < high { | |
// First half | |
if isVowel(array[low]) { | |
count += 1 | |
} | |
// second half | |
if isVowel(array[high]){ | |
count -= 1 | |
} | |
// both pointers moving towards each other | |
low += 1 | |
high -= 1 | |
} | |
return count == 0 | |
} | |
func isVowel(_ char:Character) -> Bool { | |
let vowels = ["a","e","i","o","u"] | |
return vowels.contains(char.lowercased()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment