valid-palindrome.dart (639B)
1 //Check to see if the string is a palindrome 2 //don't take into account spaces punctuation or caps. 3 //Time complexity is O(n) where n is the length of s. 4 //Runtime: 288ms Beats: 89.92% 5 //Memory: 144.5MB Beats: 84.87% 6 7 8 class Solution { 9 bool isPalindrome(String s) { 10 bool palindrome = true; 11 s = s.replaceAll(RegExp(r'[^\w\s _]+'), ''); 12 s = s.replaceAll(' ' , ''); 13 s = s.replaceAll('_' , ''); 14 s = s.toLowerCase(); 15 16 for(int i = 0 ; i < s.length / 2; i++){ 17 18 if(s[i] != s[s.length-1 - i]){ 19 palindrome = false; 20 break; 21 } 22 } 23 return palindrome; 24 } 25 }