let str = "Guten Tag!" let alph = ["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "x", "t", "u", "v", "w", "s", "y", "z"] var schtchik = 0 for index in str.characters.indices { for i in alph { if index = i { schtchik += 1 } } if schtchik > 26 { print("") } else { print("") } } 

Task with hackerrank.com You are given a string consisting of spaces and Latin letters. A string is called a pangram if it contains each of the 26 Latin letters at least once. Determine if a string is a pangram.

The question is how to solve it?

  • do you have an interview? :) - Max Mikheyenko
  • held the day before yesterday) well, there did not go a lot. and now I decided to decide for myself - DmitrievichR
  • First, get rid of the spaces, just in case, you can convert the entire line into lowercase letters; then, for example, create an array of 26 booleans, and pass the entire string to the boolean in the appropriate place true; at the end see all 26 be true - Max Mikheyenko

3 answers 3

I would solve this problem like this:

 let str = "Guten Tag!" let allowed = NSCharacterSet.init(charactersInString: "abcdefghijklmnopqrstuvwxyz") let trimmed = str.lowercaseString.componentsSeparatedByCharactersInSet(allowed.invertedSet).joinWithSeparator("") let count = NSSet.init(array: trimmed.characters.map{String($0)}).count print(count == 26 ? "Это панаграмма" : "Это не панаграмма") 

This is your bug fix version:

 let str = "Guten Tag!" let alph: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "x", "t", "u", "v", "w", "s", "y", "z"] var schtchik = 0 for letter in str.lowercaseString.characters { for i in alph { if letter == i { schtchik += 1 } } } if schtchik == 26 { print("YES") } else { print("NO") } 

    As close as possible to your version:

     let str = "qwertyuiopASDFGHJKLzxcvbnmddsfsfssdfsf" //наша строка var alph: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "x", "t", "u", "v", "w", "s", "y", "z"] //алфавит, Вы упустили "i" var counter = 0 for ch in str.lowercaseString.characters { //вариант 1 if alph.contains(ch) { //есть ли нужный символ counter += 1 alph.removeAtIndex(alph.indexOf(ch)!) //т.к. мы его уже учли, удаляем из массива, он нам больше не нужен. } //вариант 2 (на выбор) /* for i in 0..<alph.count { if ch == alph[i] { counter += 1 alph.removeAtIndex(i) break } } */ } print("\(counter == 26 ? "панаграмма" : "печалька")") 

      well, or you can dodge, and convert the line to NSSet and just see that there are 26 elements in the set

       NSString *test = @"qwertyuiopasdfghjklzxcvbnm"; test = [test stringByReplacingOccurrencesOfString:@" " withString:@""]; unsigned int len = [test length]; char buffer[len]; strncpy(buffer, [test UTF8String], len); NSMutableSet *chars = [NSMutableSet new]; for(int i = 0; i < len; ++i) { char current = buffer[i]; [chars addObject:[NSString stringWithFormat:@"%c", current]]; } NSLog(@"is %@ pangram", (chars.count == 26 ? @"" : @"NOT")); 

      Swift:

       var test: NSString = "qwertyuiopasdfghjklzxcvbnm" test = test.stringByReplacingOccurrencesOfString(" ", withString: "") let len = test.length let buffer: UnsafeMutablePointer<Int8> = UnsafeMutablePointer<Int8>.alloc(len) strncpy(buffer, test.UTF8String, len) let chars:NSMutableSet = NSMutableSet() for i in 0 ..< len { let current = buffer[i] chars.addObject(NSString.localizedStringWithFormat("%c", current)) } print("is \(chars.count == 26 ? "" : "NOT") pangram") 
      • And if on swifte - DmitrievichR
      • Well, having the code for ObzhS can already be converted. added back - Max Mikheyenko
      • Thank. another question is a more similar method (stupid \ simple \ vlob \ in many lines) solving this task with what I tried to do? - DmitrievichR
      • What I wrote in the commentary on the question can be done - Max Mikheyenko