Skip to content

Instantly share code, notes, and snippets.

@bradleyyin
Created August 22, 2019 23:34
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 bradleyyin/6b3bf110bf6db74888c42c79b279e95e to your computer and use it in GitHub Desktop.
Save bradleyyin/6b3bf110bf6db74888c42c79b279e95e to your computer and use it in GitHub Desktop.
code challenge AH 9
//"Challenge 9: Find pangrams
//Difficulty: Tricky
//
//Write a function that returns true if it is given a string that is an English pangram, ignoring letter case.
//
//Tip: A pangram is a string that contains every letter of the alphabet at least once.
//
//Sample input and output
//The string "The quick brown fox jumps over the lazy dog" should return true.
//The string "The quick brown fox jumped over the lazy dog" should return false, because it's missing the S.
//"
//
//Excerpt From: Paul Hudson. "Swift Coding Challenges." Apple Books.
import Foundation
func checkPangram(string: String) -> Bool {
let lowerCasedString = string.lowercased()
var dictionary : [Character: Int] = [:]
let alphabet = "abcdefghijklmnopqrstuvwxyz"
for character in lowerCasedString {
guard alphabet.contains(character) else { continue }
if dictionary[character] != nil {
dictionary[character]! += 1
} else {
dictionary[character] = 1
}
}
print(dictionary.count)
if dictionary.count == 26 {
return true
} else {
return false
}
}
checkPangram(string: "The quick brown fox jumps over the lazy dog")
checkPangram(string: "The quick brown fox jumped over the lazy dog")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment