Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tested with 7289505 random inputs.
- // 44 seconds
- public static bool IsStringMadeOf1(this string str, string from)
- {
- if (str.Length > from.Length) return false;
- for (int i = 0; i < str.Length; i++)
- {
- int index = from.IndexOf(str[i]);
- if (index != -1)
- {
- from = from.Remove(index, 1);
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- // 59 seconds
- public static bool IsStringMadeOf2(this string str, string from)
- {
- if (str.Length > from.Length) return false;
- Dictionary<char, int> seqSource = str.GroupBy(c => c)
- .ToDictionary(key => key.Key, value => value.Count());
- Dictionary<char, int> seqTest = from.GroupBy(c => c)
- .ToDictionary(key => key.Key, value => value.Count());
- int count;
- foreach (var kvp in seqSource)
- {
- if (!seqTest.TryGetValue(kvp.Key, out count) || kvp.Value > count) return false;
- }
- return true;
- }
- // 45 seconds
- public unsafe static bool IsStringMadeOf3(this string str, string from)
- {
- if (str.Length > from.Length) return false;
- var copy = new char[from.Length];
- from.CopyTo(0, copy, 0, copy.Length);
- fixed (char* s = str)
- {
- fixed (char* f = copy)
- {
- var l = str.Length;
- for (int i = 0; i < l; ++i)
- {
- var index = Array.IndexOf(copy, s[i]);
- if (index != -1)
- f[index] = '\0';
- else
- return false;
- }
- return true;
- }
- }
- }
- // 105 seconds
- public static bool IsStringMadeOf4(this string str, string from)
- {
- if (from.Except(str).Count() == from.Length - str.Length)
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement