Resolved common adjacent digits in both strings?

Praveen-Padamati

New member
Joined
Jul 23, 2022
Messages
4
Programming Experience
Beginner
I have two strings
string value1 = "55555551234567891234";
string value2 = "123456789123423232";
Here 1234567891234(13 digit length) is common in both string.
I have to write a condition which checks 13 common adjacent digits in both strings.
If condition satisfies then return true, or else return false.
 
Solution
An alternate take is to combine the strings with newline \n and do a regex for pattern [one or more digits (matching), followed by (lookahead) zero or more digits, newline, zero or more digits, backreference matching].
The pattern may look like this @"(\d+)(?=\d*\n\d*\1)". Then you get the longest match if any.
C#:
var pattern = @"(\d+)(?=\d*\n\d*\1)";
var m = Regex.Matches(value1 + "\n" +  value2, pattern);
var longest = m.Cast<Match>().Select(v => v.Value).OrderByDescending(v => v.Length).FirstOrDefault();
Do you have to roll your own substring comparison, or are you allowed to use the built in String.Contains() method?
 
I have to write a condition which checks 13 common adjacent digits in both strings.
You say that but it kinda seems like you want us to write it. As always, think about the logic first, before even considering writing code. How would you do it with pen and paper? The same logic can be used in code, so work out the logic first.
 
Do you have to roll your own substring comparison, or are you allowed to use the built in String.Contains() method?
Contains does not work. Because if at least 2 adjacent digits matches, it returns true which is not the expected result.
And, substring comparison can be done when there is some fixed length of string.
But in my case, the string length is dynamic.
 
You say that but it kinda seems like you want us to write it. As always, think about the logic first, before even considering writing code. How would you do it with pen and paper? The same logic can be used in code, so work out the logic first.
I have tried with String.Contains() and String.Compare() inbuilt functions.
I don't want you to write the entire code but if you can help me with an idea of logic, that would be helpful.
 
And if the idea of using dynamic programming from that link is too complex, you could use the brute force approach. In pseudo code:
C#:
for length: value1.Length..1
    for index: 0..value1.Length - length
        part = length long substring of value1 starting at index
        if part is in value2
            found it!
 
I have tried with String.Contains() and String.Compare() inbuilt functions.
I don't want you to write the entire code but if you can help me with an idea of logic, that would be helpful.
That's the thing though, it is the code that we are here to help with. The logic is not a C# problem because the logic is independent of programming language and even whether you are writing code at all. The logic would be the same if you were doing it with pen and paper. How about you put some thought into the logic first and then tell us what you've tried and where you're stuck if and when that happens? That you have tried code without knowing what the code has to do, i.e. the logic, shows that you are trying to do things backwards.
 
An alternate take is to combine the strings with newline \n and do a regex for pattern [one or more digits (matching), followed by (lookahead) zero or more digits, newline, zero or more digits, backreference matching].
The pattern may look like this @"(\d+)(?=\d*\n\d*\1)". Then you get the longest match if any.
C#:
var pattern = @"(\d+)(?=\d*\n\d*\1)";
var m = Regex.Matches(value1 + "\n" +  value2, pattern);
var longest = m.Cast<Match>().Select(v => v.Value).OrderByDescending(v => v.Length).FirstOrDefault();
 
Solution
Back
Top Bottom