|
| 1 | +/* ============================================================================== |
| 2 | + * 功能描述:KeyBoradRowSln |
| 3 | + * 创 建 者:gz |
| 4 | + * 创建日期:2017/5/9 12:44:32 |
| 5 | + * ==============================================================================*/ |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +//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. |
| 11 | + |
| 12 | + |
| 13 | +//American keyboard |
| 14 | + |
| 15 | + |
| 16 | +//Example 1: |
| 17 | +//Input: ["Hello", "Alaska", "Dad", "Peace"] |
| 18 | +//Output: ["Alaska", "Dad"] |
| 19 | +namespace HashTable.Lib |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// KeyBoradRowSln |
| 23 | + /// </summary> |
| 24 | + public class KeyBoradRowSln |
| 25 | + { |
| 26 | + public string[] FindWords(string[] words) |
| 27 | + { |
| 28 | + int[] hash = new int[123]; |
| 29 | + char[] chs1 = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'}; |
| 30 | + foreach (var ch in chs1) |
| 31 | + { |
| 32 | + hash[Convert.ToInt32(ch)] = 1; //q |
| 33 | + hash[Convert.ToInt32(ch)-32] = 1; //Q |
| 34 | + } |
| 35 | + chs1 = new[] {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; |
| 36 | + foreach (var ch in chs1) |
| 37 | + { |
| 38 | + hash[Convert.ToInt32(ch)] = 2; |
| 39 | + hash[Convert.ToInt32(ch) - 32] = 2; |
| 40 | + } |
| 41 | + chs1 = new[] {'z', 'x', 'c', 'v', 'b', 'n', 'm'}; |
| 42 | + foreach (var ch in chs1) |
| 43 | + { |
| 44 | + hash[Convert.ToInt32(ch)] = 3; |
| 45 | + hash[Convert.ToInt32(ch) - 32] = 3; |
| 46 | + } |
| 47 | + List<string> rtn = new List<string>(); |
| 48 | + foreach (var word in words) |
| 49 | + { |
| 50 | + if(string.IsNullOrEmpty(word)) continue; |
| 51 | + int row = hash[word[0]]; |
| 52 | + bool allflag = true; |
| 53 | + foreach (var ch in word) |
| 54 | + { |
| 55 | + if (hash[ch] != row) |
| 56 | + { |
| 57 | + allflag = false; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + if(allflag==true) rtn.Add(word); |
| 62 | + } |
| 63 | + return rtn.ToArray(); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | +} |
0 commit comments