[Swift]LeetCode843. 猜猜这个单词 | Guess the Word

This problem is an interactive problem new to the LeetCode platform.

We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret.

You may call master.guess(word) to guess a word.  The guessed word should have type string and must be from the original list with 6 lowercase letters.

This function returns an integer type, representing the number of exact matches (value and position) of your guess to the secret word.  Also, if your guess is not in the given wordlist, it will return -1 instead.

For each test case, you have 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or less calls to master.guess and at least one of these guesses was the secret, you pass the testcase.

Besides the example test case below, there will be 5 additional test cases, each with 100 words in the word list.  The letters of each word in those testcases were chosen independently at random from ‘a‘ to ‘z‘, such that every word in the given word lists is unique.

Example 1:
Input: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"]

Explanation:

master.guess("aaaaaa") returns -1, because "aaaaaa" is not in wordlist.
master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches.
master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches.
master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches.
master.guess("abcczz") returns 4, because "abcczz" has 4 matches.

We made 5 calls to master.guess and one of them was the secret, so we pass the test case.

Note:  Any solutions that attempt to circumvent the judge will result in disqualification.



这个问题是 LeetCode 平台新增的交互式问题 

我们给出了一个由一些独特的单词组成的单词列表,每个单词都是 6 个字母长,并且这个列表中的一个单词将被选作秘密。

你可以调用 master.guess(word) 来猜单词。你所猜的单词应当是存在于原列表并且由 6 个小写字母组成的类型字符串

此函数将会返回一个整型数字,表示你的猜测与秘密单词的准确匹配(值和位置同时匹配)的数目。此外,如果你的猜测不在给定的单词列表中,它将返回 -1

对于每个测试用例,你有 10 次机会来猜出这个单词。当所有调用都结束时,如果您对 master.guess 的调用不超过 10 次,并且至少有一次猜到秘密,那么您将通过该测试用例。

除了下面示例给出的测试用例外,还会有 5 个额外的测试用例,每个单词列表中将会有 100 个单词。这些测试用例中的每个单词的字母都是从 ‘a‘ 到 ‘z‘ 中随机选取的,并且保证给定单词列表中的每个单词都是唯一的。

示例 1:
输入: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"]

解释:

master.guess("aaaaaa") 返回 -1, 因为 "aaaaaa" 不在 wordlist 中.
master.guess("acckzz") 返回 6, 因为 "acckzz" 就是秘密,6个字母完全匹配。
master.guess("ccbazz") 返回 3, 因为 "ccbazz" 有 3 个匹配项。
master.guess("eiowzz") 返回 2, 因为 "eiowzz" 有 2 个匹配项。
master.guess("abcczz") 返回 4, 因为 "abcczz" 有 4 个匹配项。

我们调用了 5 次master.guess,其中一次猜到了秘密,所以我们通过了这个测试用例。

提示:任何试图绕过评判的解决方案都将导致比赛资格被取消。



Runtime: 8 ms

Memory Usage: 19.1 MB

 1 /**
 2  * // This is the Master‘s API interface.
 3  * // You should not implement it, or speculate about its implementation
 4  * class Master {
 5  *     public func guess(word: String) -> Int {}
 6  * }
 7  */
 8 class Solution {
 9     func findSecretWord(_ wordlist: [String], _ master: Master) {
10         var wordlist = wordlist
11         var i:Int = 0
12         var x:Int = 0
13         while(i < 10 && x < 6)
14         {
15             var guess:String = wordlist[Int.random(in:0..<wordlist.count)]
16             var x:Int = master.guess(guess)
17             var wordlist2:[String] = [String]()
18             for w in wordlist
19             {
20                 if match(guess, w) == x
21                 {
22                     wordlist2.append(w)
23                 }
24             }
25             wordlist = wordlist2
26             i += 1
27         }
28     }
29
30     func match(_ a:String,_ b:String) -> Int
31     {
32         var matches:Int = 0
33         var arrA:[Character] = Array(a)
34         var arrB:[Character] = Array(b)
35         for i in 0..<a.count
36         {
37             if arrA[i] == arrB[i]
38             {
39                 matches += 1
40             }
41         }
42         return matches
43     }
44 }

原文地址:https://www.cnblogs.com/strengthen/p/10589944.html

时间: 2024-10-13 00:10:41

[Swift]LeetCode843. 猜猜这个单词 | Guess the Word的相关文章

[Swift]LeetCode212. 单词搜索 II | Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

[Swift]LeetCode243.最短单词距离 $ Shortest Word Distance

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

[Swift]LeetCode30. 与所有单词相关联的字串 | Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input

[Swift]LeetCode748. 最短完整词 | Shortest Completing Word

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate Here, for letters we ignore case. For example, "P" on the licensePl

单词拆分 I &#183; Word Break

[抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]: [复杂度]:Time complexity: O(n*l^2+m) Space complexity: O() 查询字符串是否在词典中也是n N字符串长度 M单词个数 L单词平均长度 [英文数据结构或算法,为什么不用别的数据结构或算法]:

基于 MapReduce 的单词计数(Word Count)的实现

完整代码: // 导入必要的包 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.conf.Configuration; import org.apache.

单词缩写集 &#183; word abbreviation set

[抄题]: [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]: [复杂度]:Time complexity: O() Space complexity: O() [英文数据结构或算法,为什么不用别的数据结构或算法]: [其他解法]: [Follow Up]: [

找到一个单词的所有相似单词

相似单词为 只差一位字母的单词,练习Map容器 package chapter4; import java.util.*; import java.util.Map.Entry; /* * 说明:找到一个单词的所有相似单词 例如: wine 和 dine wind 和wing 只有一个字母不同 */ public class TreeMapTest { /* * 判断2个单词是否指差一个字母 */ public static boolean oneCharOff(String s1, Strin

大数据【四】MapReduce(单词计数;二次排序;计数器;join;分布式缓存)

   前言: 根据前面的几篇博客学习,现在可以进行MapReduce学习了.本篇博客首先阐述了MapReduce的概念及使用原理,其次直接从五个实验中实践学习(单词计数,二次排序,计数器,join,分布式缓存). 一 概述 定义 MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE).这样做的好处是可以在任务被分解后,可以通过大量机器进行并行计算,减少整个操作的时间. 适用范围:数据量大,但是数据种类小可以放入内存. 基