I was in the technical round of a campus drive yesterday and asked to solve this problem. I was not reached close to the solution of this problem and i was rejected from there , now I am still trying to solve this but not able to solve.I am wondering that how to solve this problem.The interviewer says that there will be no space in the test string and also it's a single string.
我昨天参加了校园驾驶的技术轮,并要求解决这个问题。我没有接近解决这个问题,我被拒绝了,现在我仍在努力解决这个问题,但无法解决。我想知道如何解决这个问题。面试官说不会有测试字符串中的空格,也是一个字符串。
if there will input String pattern will abba. then matching patterns will be 1) catdogdogcat 2) redbluebluered 3) noyesyesno
如果有输入字符串模式将abba。然后匹配模式将是1)catdogdogcat 2)redbluebluered 3)noyesyesno
And not matching pattern will be 1) catdogcat 2) redbluered 3) yesnoyes
而不匹配的模式将是1)catdogcat 2)redbluered 3)yesnoyes
(sorry for my bad english)
(对不起,我的英语不好)
thank you.
1 个解决方案
#1
2
This might be what you're after if you need a generic solution that doesn't use specific words:
如果您需要不使用特定单词的通用解决方案,这可能就是您所追求的:
(\w+)(\w+)\2\1
https://regex101.com/r/hP8lA3/1
It's not very efficient but it tries to greedily match two word groups and then uses backreferences to make sure they are followed by the second group first, then the first group.
它不是很有效但是它试图贪婪地匹配两个单词组,然后使用反向引用来确保它们首先跟随第二组,然后是第一组。
You could make it even more generic to match two groups of any character (besides newline): (.+)(.+)\2\1
您可以使它更通用,以匹配任何字符的两个组(除了换行符):(。+)(。+)\ 2 \ 1
Here is one possible solution to the full problem using C#:
以下是使用C#解决完整问题的一种可能解决方案:
private bool matcher(string pseudoPattern, string text) {
string regexPattern = "^";
var uniqueParts = new List<char>();
foreach (char part in pseudoPattern.ToCharArray())
if (uniqueParts.Contains(part)) {
int backReference = uniqueParts.FindIndex(p => p == part) + 1;
regexPattern += @"\" + backReference;
}
else {
uniqueParts.Add(part);
regexPattern += @"(\w+)";
}
regexPattern += "$";
var regex = new Regex(regexPattern);
return regex.Match(text).Success;
}
#1
2
This might be what you're after if you need a generic solution that doesn't use specific words:
如果您需要不使用特定单词的通用解决方案,这可能就是您所追求的:
(\w+)(\w+)\2\1
https://regex101.com/r/hP8lA3/1
It's not very efficient but it tries to greedily match two word groups and then uses backreferences to make sure they are followed by the second group first, then the first group.
它不是很有效但是它试图贪婪地匹配两个单词组,然后使用反向引用来确保它们首先跟随第二组,然后是第一组。
You could make it even more generic to match two groups of any character (besides newline): (.+)(.+)\2\1
您可以使它更通用,以匹配任何字符的两个组(除了换行符):(。+)(。+)\ 2 \ 1
Here is one possible solution to the full problem using C#:
以下是使用C#解决完整问题的一种可能解决方案:
private bool matcher(string pseudoPattern, string text) {
string regexPattern = "^";
var uniqueParts = new List<char>();
foreach (char part in pseudoPattern.ToCharArray())
if (uniqueParts.Contains(part)) {
int backReference = uniqueParts.FindIndex(p => p == part) + 1;
regexPattern += @"\" + backReference;
}
else {
uniqueParts.Add(part);
regexPattern += @"(\w+)";
}
regexPattern += "$";
var regex = new Regex(regexPattern);
return regex.Match(text).Success;
}