Skip to content

Instantly share code, notes, and snippets.

@vrat28
Created April 7, 2021 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/68afd6099f89b5606fba396d595126d8 to your computer and use it in GitHub Desktop.
Save vrat28/68afd6099f89b5606fba396d595126d8 to your computer and use it in GitHub Desktop.
Determine if two string halves are equal (Leetcode 1704)
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