valid-palindrome-ii.dart (790B)
1 //This is a solution that takes O(n^2) time 2 //which is too slow for leet code to accept. 3 //I am going to redo this using two pointers 4 //from the start and one from the end. 5 6 class Solution { 7 bool validPalindrome(String s) { 8 if(check_palindrome(s.substring(1))){ 9 return true; 10 } 11 for(int i = 1 ; i < s.length ; ++i){ 12 String new_str = s.substring(0 , i) + s.substring(i + 1); 13 if(check_palindrome(new_str) == true){ 14 return true; 15 } 16 } 17 return false; 18 } 19 bool check_palindrome(String s){ 20 if(s.length <= 1){ 21 return true; 22 } 23 for(int i = 0 ; i < s.length / 2 + 1; ++i){ 24 if(s[i] != s[s.length - 1 - i]){ 25 return false; 26 } 27 } 28 return true; 29 } 30 }