Note that there are some explanatory texts on larger screens.

plurals
  1. POReduce memory usage in the below anagram program - C#
    text
    copied!<p>Trying the Anagram checker by inputting paragraph and output is clear, but the memory limits exceeds the specified</p> <p>This the code which i tried</p> <pre><code>using System; class Program { static void Main(string[] args) { string[] arr = (Punct(Console.ReadLine()).ToLower()).Split(' '); string a = string.Empty; System.Collections.Generic.Dictionary&lt;string, string&gt; dn = new System.Collections.Generic.Dictionary&lt;string, string&gt;(); // *2 foreach (string s in arr) { string st = sort(s); if (dn.ContainsKey(st)) { if (dn[st] != s) { if (a.Contains(dn[st])) a = a.Replace(dn[st], dn[st] + " " + s); // *1 else a = a + dn[st] + " " + s + "\n"; dn[st] = s; } } else dn.Add(st, s); } Console.Write(a); } public static string sort(string s) { char[] chars = s.ToCharArray(); Array.Sort(chars); return new string(chars); } public static string Punct(string s) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (char c in s) { if (!char.IsPunctuation(c)) sb.Append(c); } return sb.ToString(); } } </code></pre> <p>On checking with profiler, String function takes a lot of memory and other dictionary too, so, my question is how can i optimize the above code to a least memory, or any codes or declaration which i am using are unnecessary ?</p> <p>Input:</p> <blockquote> <p>Parts of the world have sunlight for close to 24 hours during summer. Dan had a strap on his head to identify himself as the leader and He wondered what kind of traps lay ahead of him.</p> </blockquote> <p>Output:</p> <blockquote> <p>parts strap traps<br> dan and</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload