Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
String combinations
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var sln = new solution(); Dictionary<int, string> dctnry= new Dictionary<int, string>(); dctnry.Add(0, "abc"); dctnry.Add(1, "def"); dctnry.Add(2, "ghi"); List<string> strList = sln.GetStrCombinations(dctnry, new int[] {0,1,2}); foreach(var str in strList) { Console.WriteLine(str); } Console.WriteLine("-----------------"); strList = sln.GetStrCombinations2(dctnry, new int[] {0,1,2}); foreach(var str in strList) { Console.WriteLine(str); } } } public class solution { public List<string> GetStrCombinations(Dictionary<int, string> strDictionary, int[] keys){ List<string> strCombinations = new List<string> (); GetStrRecursive(strDictionary, ref strCombinations, keys, string.Empty); return strCombinations; } private void GetStrRecursive(Dictionary<int, string> strdctnry, ref List<string> strlist, int[] keys, string str){ if( str.Length == keys.Length) { strlist.Add(str); return; } string temp = strdctnry[keys[str.Length]]; for(int j = 0 ; j < temp.Length; j++){ GetStrRecursive(strdctnry, ref strlist, keys, str + temp[j]); } } public List<string> GetStrCombinations2(Dictionary<int, string> dctnry , int [] keys ){ List<string> strLists= new List<string>(); if ( keys.Length == 0 ) { return strLists; } var str = dctnry[keys[0]]; for (int i = 0 ; i < str.Length ; i++){ strLists.Add( "" + str[i]); } for(int i = 1 ; i < keys.Length; i++){ strLists = GetStrIterative(strLists, dctnry[keys[i]]); } return strLists; } private List<string> GetStrIterative(List<string>strList, string str){ var newStrList = new List<string> (); foreach( var s in strList){ for(int i =0; i < str.Length ; i ++){ newStrList.Add(s + str[i]); } } return newStrList; } } }
run
|
edit
|
history
|
help
0
C# Programming Exercise 3
Invalid cast
Ejercicio 1 clase 8 feb
Numeric to graphical styling using String Extension method
My world
pyramidalt
combine paths
custom Generic Que Implementation
QuickSort
Prime Number