Skip to content

Commit

Permalink
Two characters problem: C# solution
Browse files Browse the repository at this point in the history
Added a new solution for two characters problem using C# programming language.
  • Loading branch information
rasik210 committed May 23, 2018
1 parent 75db168 commit 81b0111
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
87 changes: 87 additions & 0 deletions Algorithms/Strings/Two Characters/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Problem: https://www.hackerrank.com/challenges/two-characters/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
- Get all unique characters present in the input string and their all possible combinations. e.g. for input string abdcab we need to find ab,ac,ad,bc,bd,cd combinations.
- Now for each character pair found above iterate the entire string once and check if alternating pattern is possible or not.
e.g. Let's say input string is "abdcab" and character pair is 'a','b'.
Iteration need to be done carefully
- Track the alternating pattern by keeping a track about the last character. If 'a' had come last time then next time you should encounter b.
- Ignore any other character which is not part of pair being tracked e.g. d and c in the input string during the iteration.
- Track the pattern length every time you encounter any character from the pair being tracked.
- Print the longest altenating pattern length found.
Time Complexity: O(n)
Space Complexity: O(n) //input string need to be stored in memory for repeatitive iteration to check character pair pattern
*/

using System.Collections.Generic;
using System;

class Solution
{
static void Main(string[] args)
{
//No need to capture the size of string. We will use string's length property instead.
Console.ReadLine();
var inputString = Console.ReadLine();

//first obtain all the possible combination of possible characters in the string
//e.g. for input string abdcab we need to find ab,ac,ad,bc,bd,cd combinations
var characterSet = new HashSet<char>();
var combinationList = new List<Tuple<char, char>>();
//This loop runs fixed 25! times in worst case when the string is abcdefghijkl.....xyz.
//So it'll contribute O(1) for time efficiency of the algo.
for (var i = 0; i < inputString.Length; i++)
{
if (!characterSet.Contains(inputString[i]))
{
//create all tuple combinations
foreach (var character in characterSet)
combinationList.Add(new Tuple<char, char>(inputString[i], character));
}

characterSet.Add(inputString[i]);
}

var maxPatternLength = 0;
//In worst case we iterate the input string (of length n) n * 25! times in worst case
//So overall efficiency will remain of order n.
foreach (var alternatingCharPair in combinationList)
{
//process the entire input string once for each combination if it gives max length of alternating characters
var nextExpectedChar = alternatingCharPair.Item2;
var currentPatternLength = 1;
var i = 0;
while (inputString[i] != alternatingCharPair.Item1 && inputString[i] != alternatingCharPair.Item2)
i++;

if (inputString[i] == alternatingCharPair.Item2)
nextExpectedChar = alternatingCharPair.Item1;

i++;
for (; i < inputString.Length; i++)
{
if (inputString[i] != alternatingCharPair.Item1 && inputString[i] != alternatingCharPair.Item2)
continue;

if (inputString[i] == nextExpectedChar)
{
currentPatternLength++;
nextExpectedChar = inputString[i] == alternatingCharPair.Item1 ? alternatingCharPair.Item2 : alternatingCharPair.Item1;
}
else
break;
}

if (i == inputString.Length && currentPatternLength > maxPatternLength)
//it has valid alternating pattern
maxPatternLength = currentPatternLength;

}
Console.WriteLine(maxPatternLength);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
-----|---------------- |:---------------:| --------------- | --------------- |:-------------:|:--------------:| -----
| | [Super Reduced String](https://www.hackerrank.com/challenges/reduced-string)| <ul><li>[Java](./Algorithms/Strings/Super%20Reduced%20String/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 10| ||
| | [camelCase](https://www.hackerrank.com/challenges/camelcase)| <ul><li>[Java](./Algorithms/Strings/camelCase/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 15| ||
| | [Two Characters](https://www.hackerrank.com/challenges/two-characters)| <ul><li>[Java](./Algorithms/Strings/Two%20Characters/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy| 15| ||
| | [Two Characters](https://www.hackerrank.com/challenges/two-characters)| <ul><li>[Java](./Algorithms/Strings/Two%20Characters/Solution.java)</li><li>[C#](./Algorithms/Strings/Two%20Characters/Solution.cs)</li><ul> | _O(n)_ | _O(n)_ | Easy| 15| ||
| | [Caesar Cipher](https://www.hackerrank.com/challenges/caesar-cipher-1)| <ul><li>[Java](./Algorithms/Strings/Caesar%20Cipher/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 15| ||
| | [Caesar Cipher: Encryption](https://www.hackerrank.com/challenges/linkedin-practice-caesar-cipher)| <ul><li>[Java](./Algorithms/Strings/Caesar%20Cipher%20Encryption/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 40 | ||
| | [Mars Exploration](https://www.hackerrank.com/challenges/mars-exploration)| <ul><li>[Java](./Algorithms/Strings/Mars%20Exploration/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 15| ||
Expand Down

0 comments on commit 81b0111

Please sign in to comment.