WordsGame
SRM 454 · 2009-12-05 · by Gluk
Problem Statement
You are given a
Constraints
- word will contain between 1 and 50 letters ('a'-'z', 'A'-'Z'), inclusive.
- All characters in word will be distinct.
- The number of elements in grid will be equal to the length of word.
- The length of each element of grid will be equal to the length of word.
- Each element of grid will contain only letters ('a'-'z', 'A'-'Z').
{"Mu",
"uM"}
"Mu"
Returns: 0
The first row of the grid already contains the required word.
{"sDQmQnoMGj","YafplwbBRW","fVwRishQWO","yUusOqnoCr","rAXFEfVTnS","hjVSUSWmda","PTGIcJTKqo","bQEDeeqcYC","nFolORcRKh","oXQtzHuZLW"}
"WuoDdJZbRM"
Returns: -1
{"TAfrjRZ","toAXqro","dqgqWtu","JpWketB","lULWPip","kPIDwAd","WOjyQbb"}
"xempfPT"
Returns: -1
{"GrzCmw","NTgqEn","iqPnBA","PvsYQd","BawoPg","ypmeTu"}
"iJkofN"
Returns: -1
{"hJaEzo","WdrJkk","NbdOyb","MrjSPT","kvwbVX","mQCBPj"}
"ETyAWi"
Returns: -1
{"sdf",
"bca",
"hgf"}
"abc"
Returns: 2
First we swap first and third column. Then we swap second and third column. The resulting grid contains word "abc" in the second row. There is no way to do that in less than two swaps so we return 2.
{"cdf",
"bca",
"agf"}
"abc"
Returns: 1
We could perform the same two swaps to get word "abc" in the second row. However, swapping first and third row results in a grid containing word "abc" in the first column so we return 1.
Submissions are judged against all 120 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class WordsGame with a public method int minimumSwaps(vector<string> grid, string word) · 120 test cases · 2 s / 256 MB per case