सबसे पहले, स्ट्रिंग को चेक करने के लिए सेट करें।
string s = "timetime";
अब स्ट्रिंग के दो हिस्सों के लिए दो काउंटर सेट करें।
int []one = new int[MAX_CHAR]; int []two = new int[MAX_CHAR];
स्ट्रिंग के दोनों हिस्सों की जाँच करें।
for (int i = 0, j = l - 1; i < j; i++, j--) {
one[str[i] - 'a']++;
two[str[j] - 'a']++;
} यह जांचने के लिए पूरा कोड निम्नलिखित है कि स्ट्रिंग के दोनों हिस्सों में वर्णों का एक ही सेट है या नहीं C# में।
उदाहरण
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!");
}
} आउटपुट
Yes: Two halves are same!