Created
July 31, 2018 18:16
-
-
Save sabique/a042f99248d4b6bdd1ab3feadfa9dfae to your computer and use it in GitHub Desktop.
Find one extra character in a string. -- Given two strings which are of lengths n and n+1. The second string contains all the character of the first string, but there is one extra character. Your task to find the extra character in the second string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find one extra character in a string | |
Given two strings which are of lengths n and n+1. The second string contains all the character of the first string, but there is one extra character. Your task to find the extra character in the second string. | |
Problem Link: https://www.geeksforgeeks.org/find-one-extra-character-string/ | |
Solution Time Complexity: O(n) | |
*/ | |
using System; | |
using System.Collections.Generic; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
//Assign the values to the string | |
string first = "abcdef"; | |
string second = "abcdeff"; | |
//Initialize a list to store char value | |
List<char> lst = new List<char>(); | |
//Add the characters of second string to the list | |
foreach(char c in second.ToCharArray()) | |
{ | |
lst.Add(c); | |
} | |
//Remove the characters of first string from the list | |
foreach(char c in first.ToCharArray()) | |
{ | |
lst.Remove(c); | |
} | |
//Now we have only one extra character left in the list | |
//Print the character from the list | |
foreach(char c in lst) | |
{ | |
Console.WriteLine(c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment