Skip to content

Commit f31972e

Browse files
committed
leetcode
1 parent 9fb5d9e commit f31972e

File tree

4 files changed

+271
-0
lines changed

4 files changed

+271
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
4848
- [Design Circular Queue](DesignCircularQueue/design_circular_queue.dart)
4949
- [Satisfiability of Equality Equations](SatisfiabilityOfEqualityEquations/satisfiability_of_equality_equations.dart)
5050
- [Best Time to Buy and Sell Stock](BestTimeToBuyAndSellStock/best_time_to_buy_and_sell_stock.dart)
51+
- [Valid Palindrome](ValidPalindrome/valid_palindrome.dart)
5152

5253
## Reach me via
5354

ValidPalindrome/valid_palindrome.dart

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
3+
-* Valid Palindrome *-
4+
5+
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
6+
7+
Given a string s, return true if it is a palindrome, or false otherwise.
8+
9+
10+
11+
Example 1:
12+
13+
Input: s = "A man, a plan, a canal: Panama"
14+
Output: true
15+
Explanation: "amanaplanacanalpanama" is a palindrome.
16+
Example 2:
17+
18+
Input: s = "race a car"
19+
Output: false
20+
Explanation: "raceacar" is not a palindrome.
21+
Example 3:
22+
23+
Input: s = " "
24+
Output: true
25+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
26+
Since an empty string reads the same forward and backward, it is a palindrome.
27+
28+
29+
Constraints:
30+
31+
1 <= s.length <= 2 * 105
32+
s consists only of printable ASCII characters.
33+
34+
35+
*/
36+
37+
class A {
38+
// Runtime: 580 ms, faster than 53.41% of Dart online submissions for Valid Palindrome.
39+
// Memory Usage: 147.8 MB, less than 32.95% of Dart online submissions for Valid Palindrome.
40+
bool isPalindrome(String s) {
41+
// replacing the whole string based on regex match
42+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
43+
//building a new string and splitting it and turing into a single element and reversing it and than joining to build
44+
// a single string
45+
String t = s.toString().split("").reversed.join();
46+
// if the it is same both are same than true otherwise false
47+
return s == t;
48+
}
49+
}
50+
51+
class B {
52+
// Runtime: 576 ms, faster than 55.68% of Dart online submissions for Valid Palindrome.
53+
// Memory Usage: 144.7 MB, less than 77.27% of Dart online submissions for Valid Palindrome.
54+
bool isPalindrome(String s) {
55+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
56+
int left = 0, right = s.length - 1;
57+
58+
while (left < right) {
59+
if (s[left] != s[right]) return false;
60+
61+
left++;
62+
right--;
63+
}
64+
65+
return true;
66+
}
67+
}
68+
69+
class C {
70+
// Runtime: 762 ms, faster than 11.36% of Dart online submissions for Valid Palindrome.
71+
// Memory Usage: 145.2 MB, less than 63.64% of Dart online submissions for Valid Palindrome.
72+
bool isPalindrome(String s) {
73+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
74+
int right = (s.length / 2).floor();
75+
int left = s.length % 2 == 0 ? right - 1 : right;
76+
77+
while (left >= 0 && right < s.length) {
78+
if (s[left] != s[right]) return false;
79+
80+
left--;
81+
right++;
82+
}
83+
84+
return true;
85+
}
86+
}
87+
88+
// Massive Range error
89+
class D {
90+
bool isPalindrome(String s) {
91+
bool isLetterOrDigit(String input) => RegExp(r'^[\w\d]$').hasMatch(input);
92+
93+
int i = 0, j = s.length - 1;
94+
95+
while (i < j) {
96+
while (!isLetterOrDigit(s[i])) {
97+
i++;
98+
}
99+
while (!isLetterOrDigit(s[j])) {
100+
j--;
101+
}
102+
if (!(s[i++].toLowerCase() == s[j--].toLowerCase())) {
103+
return false;
104+
}
105+
}
106+
return true;
107+
}
108+
}

ValidPalindrome/valid_palindrome.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
func isPalindrome(s string) bool {
4+
isD := func(r rune) bool {
5+
return r >= '0' && r <= '9'
6+
}
7+
isA := func(r rune) bool {
8+
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
9+
}
10+
is := func(r rune) bool {
11+
return isA(r) || isD(r)
12+
}
13+
14+
l := len(s)
15+
lt := 0
16+
rt := l - 1
17+
var empty rune
18+
for lt < rt {
19+
var f rune
20+
var l rune
21+
for lt < rt {
22+
if is(rune(s[lt])) {
23+
f = rune(s[lt])
24+
break
25+
}
26+
lt++
27+
}
28+
for lt < rt {
29+
if is(rune(s[rt])) {
30+
l = rune(s[rt])
31+
break
32+
}
33+
rt--
34+
}
35+
switch {
36+
case isA(f) && isD(l) || isD(f) && isA(l):
37+
return false
38+
case !(f-l == 32 || f-l == -32 || f-l == 0) && f != empty && l != empty:
39+
return false
40+
}
41+
lt++
42+
rt--
43+
}
44+
return true
45+
}
46+
47+
func isPalindromes(s string) bool {
48+
result := ""
49+
for i := 0; i < len(s); i++ {
50+
if isAlphaNumeric(string(s[i])) {
51+
if isNum(string(s[i])) {
52+
result += string(s[i])
53+
}
54+
if isUpperCase(string(s[i])) {
55+
result += string(lowerCase(string(s[i])))
56+
} else {
57+
result += string(s[i])
58+
}
59+
}
60+
}
61+
return palindrom(string(result))
62+
}
63+
64+
func isAlphaNumeric(s string) bool {
65+
if ('0' <= s[0] && s[0] <= '9') || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') {
66+
return true
67+
}
68+
return false
69+
}
70+
71+
func isUpperCase(s string) bool {
72+
if s[0] >= 'A' && s[0] <= 'Z' {
73+
return true
74+
}
75+
return false
76+
}
77+
78+
func isNum(s string) bool {
79+
if s[0] >= '0' && s[0] <= '9' {
80+
return true
81+
}
82+
return false
83+
}
84+
85+
func lowerCase(s string) byte {
86+
return s[0] - 'A' + 'a'
87+
}
88+
89+
func palindrom(s string) bool {
90+
for i := 0; i < len(s)/2; i++ {
91+
if s[i] != s[len(s)-1-i] {
92+
return false
93+
}
94+
}
95+
return true
96+
}

ValidPalindrome/valid_palindrome.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ✅ DART || 4 solutions || simple fast easy
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
// Runtime: 580 ms, faster than 53.41% of Dart online submissions for Valid Palindrome.
8+
// Memory Usage: 147.8 MB, less than 32.95% of Dart online submissions for Valid Palindrome.
9+
bool isPalindrome(String s) {
10+
// replacing the whole string based on regex match
11+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
12+
//building a new string and splitting it and turing into a single element and reversing it and than joining to build
13+
// a single string
14+
String t = s.toString().split("").reversed.join();
15+
// if the it is same both are same than true otherwise false
16+
return s == t;
17+
}
18+
}
19+
```
20+
21+
## Solution - 2
22+
23+
```dart
24+
class Solution {
25+
// Runtime: 576 ms, faster than 55.68% of Dart online submissions for Valid Palindrome.
26+
// Memory Usage: 144.7 MB, less than 77.27% of Dart online submissions for Valid Palindrome.
27+
bool isPalindrome(String s) {
28+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
29+
int left = 0, right = s.length - 1;
30+
31+
while (left < right) {
32+
if (s[left] != s[right]) return false;
33+
34+
left++;
35+
right--;
36+
}
37+
38+
return true;
39+
}
40+
}
41+
```
42+
43+
## Solution - 3
44+
45+
```dart
46+
class Solution {
47+
// Runtime: 762 ms, faster than 11.36% of Dart online submissions for Valid Palindrome.
48+
// Memory Usage: 145.2 MB, less than 63.64% of Dart online submissions for Valid Palindrome.
49+
bool isPalindrome(String s) {
50+
s = s.toLowerCase().replaceAll(RegExp("[^a-zA-Z0-9]"), "");
51+
int right = (s.length / 2).floor();
52+
int left = s.length % 2 == 0 ? right - 1 : right;
53+
54+
while (left >= 0 && right < s.length) {
55+
if (s[left] != s[right]) return false;
56+
57+
left--;
58+
right++;
59+
}
60+
61+
return true;
62+
}
63+
}
64+
```
65+
66+
## [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)