Sobes.tech
Junior — Middle

Kuidas Dartis saab kontrollida, kas string loeb ühepoolselt ja teispoolselt sama?

sobes.tech AI

Vastus AI-lt

In Dart, you can check if a string is a palindrome (reads the same from left to right and right to left) in several ways:

  1. Compare the string with its reversed copy:
bool isPalindrome(String s) {
  String reversed = s.split('').reversed.join('');
  return s == reversed;
}
  1. Use two pointers — one from the start, the other from the end, and compare characters alternately:
bool isPalindrome(String s) {
  int left = 0;
  int right = s.length - 1;
  while (left < right) {
    if (s[left] != s[right]) return false;
    left++;
    right--;
  }
  return true;
}

Both methods are effective for checking if a string is a palindrome.