編寫一個算法來檢查給定字符串s
是否可以由另外兩個字符串part1
和part2
組成。合併字符串檢查器(自定義規則)
限制是part1
和part2
中的字符與s
中的字符順序相同。
例子:
'codewars' is a merge from 'cdw' and 'oears':
s: c o d e w a r s = codewars
part1: c d w = cdw
part2: o e a r s = oears
但在測試案例,它不是那麼容易使其工作..
規則說,他們必須是相同的長度和相同的順序,但隨機測試用例被放入隨機順序中。 所以規則與規則衝突,所以我如何使它工作?
[Test]
public void SadPath2()
{
Assert.IsFalse(StringMerger.isMerge("codewars", "code", "wasr"), "Codewars can't be created from code and wasr");
}
SadPath2 == false;
[Test]
public void SadPath1()
{
Assert.IsFalse(StringMerger.isMerge("codewars", "cod", "wars"), "Codewars are not codwars");
}
SadPath1 == false;
[Test]
public void HappyPath2()
{
Assert.IsTrue(StringMerger.isMerge("codewars", "cdwr", "oeas"), "codewars can be created from cdwr and oeas");
}
HappyPath2 == true;
[Test]
public void HappyPath1()
{
Assert.IsTrue(StringMerger.isMerge("codewars", "code", "wars"), "codewars can be created from code and wars");
}
HappyPath1 == true;
可能不遵守規則的徹底一些隨機測試:
遵循的規則測試用例
[Test]
public void RandomTest()
{
Assert.IsTrue(StringMerger.isMerge("[W`meSnw(R1qaLLqc[=]=UAvTa_3%", "W`mnwqaLL]=va%", "[eS(R1qc[=UAT_3"), "'[W`meSnw(R1qaLLqc[=]=UAvTa_3%' is a merge of 'W`mnwqaLL]=va%' and '[eS(R1qc[=UAT_3'");
}
RandomTest == true;
[Test]
public void RandomTest2()
{
Assert.IsTrue(StringMerger.isMerge("]ftUNn7-XoX4AZ3i1+", "U7oX4A1+", "]ftNn-XZ3i"), "']ftUNn7-XoX4AZ3i1+' is a merge of 'U7oX4A1+' and ']ftNn-XZ3i");
}
RandomTest2 == true;
第一個例子在我的課堂,它可以成功地運行所有測試SadPath2
:
public class StringMerger
{
public static bool isMerge(string s, string part1, string part2)
{
int num = 0;
string txt1 = "";
Console.WriteLine("S - " + s + " - P1 - " + part1 + " - P2 - " + part2);
#region Sorting
foreach (var itm in s)
{
if (part1.Contains(itm.ToString()))
num++;
else if (part2.Contains(itm.ToString()))
num++;
}
#endregion
if (num == s.Length && num == (part1.Length + part2.Length))
return true;
return false;
}
}
第二個例子在那裏可以運行所有除了隨機測試:用Kvam
答案
public class StringMerger
{
public static bool isMerge(string s, string part1, string part2)
{
int num = 0;
int P1 = 0;
int P2 = 0;
bool p2 = false;
string txt1 = "";
#region Sorting
foreach (var itm in s)
{
if (part1.Contains(itm.ToString()))
num++;
else if (part2.Contains(itm.ToString()))
num++;
try
{
if (p2 == false)
{
txt1 = txt1 + GetLetterP1(part1, itm.ToString(), P1);
P1++;
p2 = true;
}
else
{
txt1 = txt1 + GetLetterP2(part2, itm.ToString(), P2);
P2++;
p2 = false;
}
}
catch { }
}
#endregion
if (num == s.Length && num == (part1.Length + part2.Length))
{
if (s.Contains(part1 + part2))
return true;
else if (txt1 == s)
return true;
}
return false;
}
static string GetLetterP1(string p1, string letter, int n)
{
string itm = "";
if (p1.Contains(letter))
itm = p1[n].ToString();
return itm;
}
static string GetLetterP2(string p2, string letter, int n)
{
string itm = "";
if (p2.Contains(letter))
itm = p2[n].ToString();
return itm;
}
}
結果:(使用最新的補丁)
結果用Max
答案:(用最新的修補程序)
結果與codersl
答:(使用最新的補丁)
讓我得到這個直。你有一個從兩個字符串合併的字符串,你想檢查一個特定的字符串是否被兩個字符串合併爲輸入?防爆。用你的例子。'codewars'是'code'和'wars'的合併,所以對'codewars','code','wars'進行檢查應該返回true,其中'codewars','cdoe','wars'應該返回false ? – Bauss
是的,這是正確的。 – ArchAngel
@Bauss從第一行問題來看:「'codewars'是'cdw'和'oears'的合併,所以不僅子字符串,而且還包括子合約 – Max