Last active
March 7, 2019 21:57
-
-
Save GuruRAM/e431df70066d3256d7295e6071ae2f96 to your computer and use it in GitHub Desktop.
Algo.cs
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace AlgoSolution | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var a = GetAllCombinations("1234").ToArray(); | |
| Console.WriteLine("Hello World!"); | |
| } | |
| //Rotate array 90 degrees: | |
| //123 741 | |
| //456 => 852 | |
| //789 963 | |
| static int[][] RotateArray(int[][] array) | |
| { | |
| var length = array.Length; | |
| var n = length / 2; | |
| for (var k = 0; k < n; k++) | |
| { | |
| for (var i = k; i < length - k - 1; i++) | |
| { | |
| var a1 = array[i][k]; | |
| var a2 = array[length - k][i]; | |
| var a3 = array[length - i][length - k]; | |
| var a4 = array[k][length - i]; | |
| array[i][k] = a4; | |
| array[length - k][i] = a1; | |
| array[length - i][length - k] = a2; | |
| array[k][length - i] = a3; | |
| } | |
| } | |
| return array; | |
| } | |
| public static IEnumerable<string> GetAllCombinations(string word) | |
| { | |
| return GetAllCombinations(word.ToCharArray(), 0); | |
| } | |
| public static IEnumerable<string> GetAllCombinations(char[] word, int start) | |
| { | |
| if (start > word.Length) | |
| yield break; | |
| if (start == word.Length) | |
| { | |
| yield return new string(word); | |
| yield break; | |
| } | |
| for (var i = start; i < word.Length; i++) | |
| { | |
| var tempStart = word[start]; | |
| var tempEnd = word[i]; | |
| word[start] = tempEnd; | |
| word[i] = tempStart; | |
| foreach (var res in GetAllCombinations(word, start + 1)) | |
| { | |
| yield return res; | |
| } | |
| word[start] = tempStart; | |
| word[i] = tempEnd; | |
| } | |
| } | |
| //Requirements: | |
| // Only letters are allowed | |
| // aaaabbcc -> a4b2c2 | |
| // abbbbc -> a1b4c1 | |
| //if result is longer or equal then input, return input | |
| // aabb -> a2b2 -> aabb | |
| // ab -> a1b1 -> ab | |
| // input is not empty | |
| public string EncodeString(string input) | |
| { | |
| var result = ""; | |
| for (int i = 0; i < input.Length; i++) | |
| { | |
| var counter = 1; | |
| var current = input[i]; | |
| for (int j = i + 1; j < input.Length; j++) | |
| { | |
| if (current == input[j]) | |
| counter++; | |
| else | |
| break; | |
| } | |
| result += current + counter.ToString(); | |
| i = i + counter - 1; | |
| } | |
| if (result.Length >= input.Length) | |
| return input; | |
| return result; | |
| } | |
| public string EncodeStringImproved(string input) | |
| { | |
| var result = ""; | |
| var processed = input[0]; | |
| var counter = 1; | |
| for (int i = 1; i < input.Length; i++) | |
| { | |
| var current = input[i]; | |
| if (current == processed) | |
| counter++; | |
| else | |
| { | |
| result += current + counter.ToString(); | |
| counter = 1; | |
| } | |
| processed = current; | |
| } | |
| result += result + processed + counter; | |
| if (result.Length >= input.Length) | |
| return input; | |
| return result; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment