[Swift]LeetCode500. 键盘行 | Keyboard Row
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9806211.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
示例:
输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"]
注意:
- 你可以重复使用键盘上同一字符。
- 你可以假设输入的字符串将只包含字母。
12ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 //char作key;行数作value 4 var map:[Character:Int] = [Character:Int]() 5 var res:[String] = [String]() 6 //键盘三行 7 var row1:String = "qwertyuiop" 8 var row2:String = "asdfghjkl" 9 var row3:String = "zxcvbnm" 10 makeMap(row1,1,&map) 11 makeMap(row2,2,&map) 12 makeMap(row3,3,&map) 13 for word in words 14 { 15 //转换为小写 16 var str = word.lowercased() 17 //获取行数 18 let rowNum:Int = map[str[str.startIndex]]! 19 var checkAdd:Bool = true 20 for char in str.characters 21 { 22 if rowNum != map[char] 23 { 24 checkAdd = false 25 break 26 } 27 } 28 if checkAdd 29 { 30 res.append(word) 31 } 32 } 33 return res 34 } 35 //用于初始化字典 36 func makeMap(_ str:String,_ num:Int,_ map :inout [Character:Int]) 37 { 38 for char in str.characters 39 { 40 map[char] = num 41 } 42 } 43 }
8ms
1 class Solution { 2 3 var keyboardRows = ["qwertyuiopQWERTYUIOP","asdfghjklASDFGHJKL","zxcvbnmZXCVBNM"] 4 5 func findWords(_ words: [String]) -> [String] { 6 7 var correctWords = [String]() 8 9 var shouldAdd = true 10 11 for word in words { 12 let row = findRow(for: word) 13 print("Trying: \(word)") 14 shouldAdd = true 15 for character in word { 16 if !keyboardRows[row].contains(character) { 17 print("Failed in: \(character) for row: \(row)") 18 shouldAdd = false 19 } 20 } 21 if shouldAdd { 22 correctWords.append(word) 23 print("CorrectWords: \(correctWords)") 24 } 25 } 26 27 return correctWords 28 } 29 30 func findRow(for word: String) -> Int { 31 for i in 0..<keyboardRows.count { 32 if keyboardRows[i].contains(word.first!){ 33 return i 34 } 35 } 36 return 0 37 } 38 }
12ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 guard words.count > 0 else { return [] } 4 var dict: [Int : String] = [ 5 1:String("QWERTYUIOP".sorted()), 6 2:String("ASDFGHJKL".sorted()), 7 3:String("ZXCVBNM".sorted()) 8 ] 9 var output: [String] = [] 10 for str in words { 11 let processedStr = Set(str.uppercased()).sorted() 12 guard processedStr.count > 0 else { 13 output.append(str) 14 continue 15 } 16 var key = 0 17 if dict[1]!.contains(processedStr[0]) { 18 key = 1 19 } else if dict[2]!.contains(processedStr[0]) { 20 key = 2 21 } else if dict[3]!.contains(processedStr[0]) { 22 key = 3 23 } 24 var valid = true 25 for ch in processedStr { 26 if !dict[key]!.contains(ch) { 27 valid = false 28 } 29 } 30 if valid { 31 output.append(str) 32 } 33 } 34 return output 35 } 36 }
12ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 let rowOne = "qwertyuiop" 4 let rowTwo = "asdfghjkl" 5 let rowThree = "zxcvbnm" 6 7 return words.filter { contain(rowOne, $0) || contain(rowTwo, $0) || contain(rowThree, $0) } 8 } 9 10 private func contain(_ str: String, _ word: String) -> Bool { 11 return word.lowercased().filter { !str.contains($0) }.count == 0 12 } 13 }
16ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 var row1: Set<String> = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"] 4 var row2: Set<String> = ["A", "S", "D", "F", "G", "H", "J", "K", "L"] 5 var row3: Set<String> = ["Z", "X", "C", "V", "B", "N", "M"] 6 var ret: [String] = [] 7 8 for str in words { 9 let char = str[str.index(str.startIndex, offsetBy: 0)] 10 var sameRow = true 11 var currentRow: Set<String> 12 if row1.contains(String(char).capitalized) { currentRow = row1 } 13 else if row2.contains(String(char).capitalized) { currentRow = row2 } 14 else { currentRow = row3 } 15 16 for char in str { 17 if !currentRow.contains(String(char).capitalized) { sameRow = false } 18 } 19 20 if sameRow { ret.append(str) } 21 } 22 23 return ret 24 } 25 }
20ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 let line1 : String = "qwertyuiop" 4 let line2 : String = "asdfghjkl" 5 let line3 : String = "zxcvbnm" 6 var results : [String] = [] 7 8 var containLine : Int = 0 9 for word in words { 10 containLine = 0 11 for w in word { 12 if line1.contains(w) { 13 if containLine == 0 { 14 containLine = 1 15 }else if containLine != 1{ 16 containLine = -1 17 break 18 } 19 continue 20 } 21 if line2.contains(w) { 22 if containLine == 0 { 23 containLine = 2 24 }else if containLine != 2{ 25 containLine = -1 26 break 27 } 28 continue 29 } 30 if line3.contains(w) { 31 if containLine == 0 { 32 containLine = 3 33 }else if containLine != 3{ 34 containLine = -1 35 break 36 } 37 continue 38 } 39 } 40 if containLine != -1 { 41 results.append(word) 42 } 43 } 44 return results 45 } 46 }
28ms
1 class Solution { 2 func findWords(_ words: [String]) -> [String] { 3 var res = [String]() 4 let dic = ["q":0,"w":0,"e":0,"r":0,"t":0,"y":0,"u":0,"i":0,"o":0,"p":0, 5 "a":1,"s":1,"d":1,"f":1,"g":1,"h":1,"j":1,"k":1,"l":1, 6 "z":2,"x":2,"c":2,"v":2,"b":2,"n":2,"m":2,] 7 for s in words{ 8 let low = s.lowercased() 9 var line:Int? 10 var bool = true 11 for c in low{ 12 let num = dic[String(c)] 13 if line == nil{ 14 line = num 15 }else if line != num{ 16 bool = false 17 break 18 } 19 } 20 if bool { 21 res.append(s) 22 } 23 } 24 return res 25 } 26 }