String Reversal और Palindrome
In this page:
reversed_text = text[::-1]
left, right = 0, len(text) - 1
while left < right:
if text[left] != text[right]:
return False
left += 1
right -= 1
return True
String Reversal
किसी string को reverse करने का मतलब है उन्हीं characters के साथ opposite order में एक नई string produce करना, आखिरी character से पहले तक, जो आखिर से पीछे की ओर एक नई string बनाकर या characters को जगह पर swap करके किया जा सकता है।
उदाहरण: String Reversal
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s = "hello";
reverse(s.begin(), s.end());
cout << "Reversed: " << s << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "hello";
String reversed = new StringBuilder(s).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
s = "hello"
print("Reversed:", s[::-1])
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "hello";
int n = strlen(s);
for (int i = 0; i < n / 2; i++) {
char t = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i] = t;
}
printf("Reversed: %s\n", s);
return 0;
}
Login to try C/C++/Java code in the editor
Palindrome Basics
एक palindrome एक string है जो आगे और पीछे दोनों तरह से identically पढ़ती है, जैसे level या racecar। एक की जांच करने का मतलब है string की तुलना इसके खुद के reverse से करना, या दोनों ends से अंदर की ओर characters compare करना।
उदाहरण: Palindrome Basics
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s = "racecar";
string rev = s;
reverse(rev.begin(), rev.end());
cout << (s == rev ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "racecar";
String rev = new StringBuilder(s).reverse().toString();
System.out.println(s.equals(rev) ? "Palindrome" : "Not a palindrome");
}
}
s = "racecar"
print("Palindrome" if s == s[::-1] else "Not a palindrome")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "racecar";
int n = strlen(s), isPal = 1;
for (int i = 0; i < n / 2; i++) if (s[i] != s[n - 1 - i]) isPal = 0;
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Two Pointer Method
Two-pointer method कभी एक reversed copy बनाए बिना एक palindrome जांचती है: एक pointer front से शुरू होता है, एक back से, और वे middle की ओर move करते हुए characters compare करते हैं, एक mismatch मिलते ही जल्दी रुकते हुए।
उदाहरण: Two Pointer Method
#include <iostream>
using namespace std;
int main() {
string s = "level";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (s[left] != s[right]) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "level";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome" : "Not a palindrome");
}
}
s = "level"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if s[left] != s[right]:
is_pal = False
break
left += 1; right -= 1
print("Palindrome" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "level";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (s[left] != s[right]) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Ignoring Case
एक palindrome check को case-insensitive बनाने के लिए, compare हो रहे दोनों characters को compare करने से पहले एक जैसे case (आमतौर पर lowercase) में convert करें, ताकि Level को अब भी सही से एक palindrome पहचाना जाए।
उदाहरण: Ignoring Case
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string s = "Level";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (tolower(s[left]) != tolower(s[right])) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome (case-insensitive)" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "Level";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome (case-insensitive)" : "Not a palindrome");
}
}
s = "Level"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if s[left].lower() != s[right].lower():
is_pal = False
break
left += 1; right -= 1
print("Palindrome (case-insensitive)" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[] = "Level";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (tolower(s[left]) != tolower(s[right])) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome (case-insensitive)" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
Palindrome Practice
Palindrome problems careful index management practice करने का एक बढ़िया तरीका हैं, क्योंकि string की boundaries पर off-by-one errors सबसे आम bug हैं, खासकर non-alphanumeric characters भी skip करते समय।
उदाहरण: Palindrome Practice
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string s = "A man a plan a canal Panama";
int left = 0, right = s.length() - 1;
bool isPal = true;
while (left < right) {
if (!isalnum(s[left])) { left++; continue; }
if (!isalnum(s[right])) { right--; continue; }
if (tolower(s[left]) != tolower(s[right])) { isPal = false; break; }
left++; right--;
}
cout << (isPal ? "Palindrome" : "Not a palindrome") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "A man a plan a canal Panama";
int left = 0, right = s.length() - 1;
boolean isPal = true;
while (left < right) {
if (!Character.isLetterOrDigit(s.charAt(left))) { left++; continue; }
if (!Character.isLetterOrDigit(s.charAt(right))) { right--; continue; }
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { isPal = false; break; }
left++; right--;
}
System.out.println(isPal ? "Palindrome" : "Not a palindrome");
}
}
s = "A man a plan a canal Panama"
left, right = 0, len(s) - 1
is_pal = True
while left < right:
if not s[left].isalnum():
left += 1; continue
if not s[right].isalnum():
right -= 1; continue
if s[left].lower() != s[right].lower():
is_pal = False
break
left += 1; right -= 1
print("Palindrome" if is_pal else "Not a palindrome")
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[] = "A man a plan a canal Panama";
int left = 0, right = strlen(s) - 1, isPal = 1;
while (left < right) {
if (!isalnum(s[left])) { left++; continue; }
if (!isalnum(s[right])) { right--; continue; }
if (tolower(s[left]) != tolower(s[right])) { isPal = 0; break; }
left++; right--;
}
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Login to try C/C++/Java code in the editor
- String को जगह पर reverse करना और फिर इसे बदली हुई string से compare करना, इसलिए एक palindrome check हमेशा yes कहता है।
- दोनों pointers को cross होने पर रुकने के बजाय
left < s.length()तक loop करना, जो pairs दो बार compare करता है। - Problem के ignore करने को कहने पर case या spaces handle करना भूल जाना, इसलिए
"Racecar"को एक palindrome नहीं report किया जाता।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: