सबसे पहले, Check की जाने वाली String Set करें।
string s = "timetime";
अब String के दो Halves के लिए दो Counters Set करें।
int[] one = new int[MAX_CHAR]; int[] two = new int[MAX_CHAR];
String के दोनों Halves Check करें।
for (int i = 0, j = l - 1; i < j; i++, j--) { one[str[i] - 'a']++; two[str[j] - 'a']++; }निम्न Complete Code C# में Check करने के लिए कि String के दोनों Halves में Same Set of Characters हैं या नहीं।
Example
using System; class Demo { static int MAX_CHAR = 26; static bool findSameCharacters(string str) { int[] one = new int[MAX_CHAR]; int[] two = new int[MAX_CHAR]; int l = str.Length; if (l == 1) return true; for (int i = 0, j = l - 1; i < j; i++, j--) { one[str[i] - 'a']++; two[str[j] - 'a']++; } for (int i = 0; i < MAX_CHAR; i++) if (one[i] != two[i]) return false; return true; } public static void Main() { string str = "timetime"; if (findSameCharacters(str)) Console.Write("Yes: Two halves are same!"); else Console.Write("No! Two halves are not same!"); } }Output
Yes: Two halves are same!