Created
August 3, 2020 18:22
-
-
Save sedmo/b4416d7efbe7963da1ac12a0b2a0bf44 to your computer and use it in GitHub Desktop.
Week Day 3 August 2020 leetcode challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
void printArray(vector<char> arr){ | |
cout<<"["; | |
for(auto c: arr){ | |
cout<<c<<","; | |
} | |
cout<<"]"<<endl; | |
} | |
vector<char> stringToArrLower(string s){ | |
vector<char> arr; | |
for(auto ch: s){ | |
if(isalnum(ch)){ | |
char next = isalpha(ch) ? tolower(ch) : ch; | |
arr.push_back(next); | |
} | |
} | |
return arr; | |
} | |
public: | |
//runtime : O(1) | space: O(n) | |
bool isPalindrome(string s) { | |
vector<char> arr; | |
arr = stringToArrLower(s); | |
int l = 0; | |
int r = arr.size()-1; | |
while(l<r){ | |
if(arr.at(l) != arr.at(r)){ | |
return false; | |
} | |
l++; | |
r--; | |
} | |
return true; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment