commit e397b22b7466bdf988847742f089f7e943e78785 parent 5d7859c2eaf3acda006d2625fa521f4e465a4d7f Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sat, 22 Apr 2023 14:29:54 -0500 Completed valid palindrome ii with dart Diffstat:
| A | valid-palindrome-ii/valid-palindrome-ii.dart | | | 30 | ++++++++++++++++++++++++++++++ |
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git a/valid-palindrome-ii/valid-palindrome-ii.dart b/valid-palindrome-ii/valid-palindrome-ii.dart @@ -0,0 +1,30 @@ +//This is a solution that takes O(n^2) time +//which is too slow for leet code to accept. +//I am going to redo this using two pointers +//from the start and one from the end. + +class Solution { + bool validPalindrome(String s) { + if(check_palindrome(s.substring(1))){ + return true; + } + for(int i = 1 ; i < s.length ; ++i){ + String new_str = s.substring(0 , i) + s.substring(i + 1); + if(check_palindrome(new_str) == true){ + return true; + } + } + return false; + } + bool check_palindrome(String s){ + if(s.length <= 1){ + return true; + } + for(int i = 0 ; i < s.length / 2 + 1; ++i){ + if(s[i] != s[s.length - 1 - i]){ + return false; + } + } + return true; + } +}