none
need help to solve the requirement RRS feed

  • Question

  • I have 2 strings 

    for ex: str1 is ABC and str2 is BC . The result should come as an array by returning A in 1st one empty as the second one because BC exist in str1 and str2

    ex2 : str1 BC str2 is BANGALORE. OP should be in array 1st one is C and 2nd one is ANGALORE because B exist in both. 

    Friday, April 3, 2020 6:54 AM

Answers

  • Hi Sai Pranav,

    Thank you for posting here.

    In order to do this, two main steps are required.

    1. Find the longest identical string in the two strings.

    2. Replace this string in the two strings with empty.

            static void Main()
            {
                string str1 = "ABC";
                string str2 = "BC";
    
                var res = getDuplicate(str1, str2);
                foreach (var item in res)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            public static string[] getDuplicate(string str1,string str2) 
            {
                string[] strs = new string[2];
                var substrings =
                   from start in Enumerable.Range(0, str1.Length)
                   from end in Enumerable.Range(0, str1.Length - start + 1)
                   select str1.Substring(start, end);
    
                var matching = substrings.Where(item => str2.Contains(item));
    
                var longest = matching.OrderByDescending(s => s.Length).First();
                strs[0] = str1.Replace(longest, "");
                strs[1] = str2.Replace(longest, "");
                return strs;
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sai Pranav Tuesday, April 7, 2020 7:03 PM
    Friday, April 3, 2020 8:29 AM

All replies

  • Hi Sai Pranav,

    Thank you for posting here.

    In order to do this, two main steps are required.

    1. Find the longest identical string in the two strings.

    2. Replace this string in the two strings with empty.

            static void Main()
            {
                string str1 = "ABC";
                string str2 = "BC";
    
                var res = getDuplicate(str1, str2);
                foreach (var item in res)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            public static string[] getDuplicate(string str1,string str2) 
            {
                string[] strs = new string[2];
                var substrings =
                   from start in Enumerable.Range(0, str1.Length)
                   from end in Enumerable.Range(0, str1.Length - start + 1)
                   select str1.Substring(start, end);
    
                var matching = substrings.Where(item => str2.Contains(item));
    
                var longest = matching.OrderByDescending(s => s.Length).First();
                strs[0] = str1.Replace(longest, "");
                strs[1] = str2.Replace(longest, "");
                return strs;
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sai Pranav Tuesday, April 7, 2020 7:03 PM
    Friday, April 3, 2020 8:29 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 7, 2020 9:33 AM