Skip to content

Commit 6115dbe

Browse files
committed
Solved leetcode: strings
1 parent 2b2e9c3 commit 6115dbe

File tree

6 files changed

+274
-0
lines changed

6 files changed

+274
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package algorithm.easy.p125;
2+
3+
/**
4+
* https://leetcode.com/problems/valid-palindrome/
5+
* 2 ms / 39 MB
6+
*/
7+
public class Solution {
8+
public boolean isPalindrome(String s) {
9+
s = extractAlphaNumeric(s);
10+
if (s.isEmpty()) {
11+
return true;
12+
}
13+
return checkPalindrome(s);
14+
}
15+
16+
public static String extractAlphaNumeric(String s) {
17+
StringBuilder result = new StringBuilder(s.length());
18+
int upperLowerGap = 'a' - 'A';
19+
for (int i = 0; i < s.length(); i++) {
20+
char ch = s.charAt(i);
21+
if (ch >= 'A' && ch <= 'Z') {
22+
result.append((char) (ch + upperLowerGap));
23+
continue;
24+
}
25+
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
26+
result.append(ch);
27+
}
28+
}
29+
return result.toString();
30+
}
31+
32+
// public static String extractAlphaNumeric(String s) {
33+
// StringBuilder result = new StringBuilder(s.length());
34+
// for (int i = 0; i < s.length(); i++) {
35+
// char ch = s.charAt(i);
36+
// if (Character.isAlphabetic(ch) || Character.isDigit(ch)) {
37+
// result.append(Character.toLowerCase(ch));
38+
// }
39+
// }
40+
// return result.toString();
41+
// }
42+
43+
public static boolean checkPalindrome(String s) {
44+
int half = s.length() / 2;
45+
for (int i = 0; i < half; i++) {
46+
char cur = s.charAt(i);
47+
char comp = s.charAt(s.length() - i - 1);
48+
if (cur != comp) {
49+
return false;
50+
}
51+
}
52+
return true;
53+
}
54+
55+
public static void main(String[] args) {
56+
String[] examples = {
57+
"A man, a plan, a canal: Panama",
58+
"race a car",
59+
" "
60+
};
61+
62+
for (String example : examples) {
63+
System.out.println("Check: " + example + " >> " + new Solution().isPalindrome(example));
64+
}
65+
}
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package algorithm.easy.p125;
2+
3+
import java.util.*;
4+
5+
/**
6+
* https://leetcode.com/problems/valid-palindrome/
7+
* 3 ms / 38.9 MB
8+
*/
9+
public class SolutionByDequeue {
10+
11+
public boolean isPalindrome(String s) {
12+
Deque<Character> deque = new ArrayDeque<>(s.length());
13+
14+
for (int i = 0; i < s.length(); i++) {
15+
char ch = s.charAt(i);
16+
if (!isAlphaNumeric(ch)) {
17+
continue;
18+
}
19+
deque.addLast(Character.toLowerCase(ch));
20+
}
21+
22+
if (deque.isEmpty()) {
23+
return true;
24+
}
25+
26+
while (!deque.isEmpty()) {
27+
Character left = deque.pollFirst();
28+
Character right = deque.pollLast();
29+
if (right == null) {
30+
return true;
31+
}
32+
if (!left.equals(right)) {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
39+
public static boolean isAlphaNumeric(char ch) {
40+
return (ch >= 'A' && ch <= 'Z')
41+
|| (ch >= 'a' && ch <= 'z')
42+
|| (ch >= '0' && ch <= '9');
43+
}
44+
45+
public static void main(String[] args) {
46+
String[] examples = {
47+
"A man, a plan, a canal: Panama",
48+
"race a car",
49+
" "
50+
};
51+
52+
for (String example : examples) {
53+
System.out.println("Check: " + example + " >> " + new SolutionByDequeue().isPalindrome(example));
54+
}
55+
}
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package algorithm.easy.p125;
2+
3+
/**
4+
* https://leetcode.com/problems/valid-palindrome/
5+
* 3 ms / 39.1 MB
6+
*/
7+
public class SolutionByReverse {
8+
public boolean isPalindrome(String s) {
9+
StringBuilder result = new StringBuilder(s.length());
10+
for (int i = 0; i < s.length(); i++) {
11+
char ch = s.charAt(i);
12+
if (ch >= 'A' && ch <= 'Z') {
13+
result.append(Character.toLowerCase(ch));
14+
continue;
15+
}
16+
if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
17+
result.append(ch);
18+
}
19+
}
20+
21+
String origin = result.toString();
22+
String reverse = result.reverse().toString();
23+
24+
return origin.isEmpty() || origin.equals(reverse);
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package algorithm.easy.p344;
2+
3+
/**
4+
* https://leetcode.com/problems/reverse-string/
5+
* 1 ms / 46 MB
6+
*/
7+
public class Solution {
8+
9+
public void reverseString(char[] s) {
10+
int len = s.length / 2;
11+
for (int i = 0; i < len; i++) {
12+
int ridx = s.length - i - 1;
13+
char t = s[i];
14+
s[i] = s[ridx];
15+
s[ridx] = t;
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
char[] s = { 'h', 'e', 'l', 'l', 'o' };
21+
new Solution().reverseString(s);
22+
System.out.println(s);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package algorithm.easy.p344;
2+
3+
/**
4+
* https://leetcode.com/problems/reverse-string/
5+
* 1 ms / 46 MB
6+
*/
7+
public class SolutionByTwoPoint {
8+
public void reverseString(char[] s) {
9+
int left = 0, right = s.length - 1;
10+
while (left < right) {
11+
char t = s[left];
12+
s[left] = s[right];
13+
s[right] = t;
14+
left++;
15+
right--;
16+
}
17+
}
18+
19+
public static void main(String[] args) {
20+
char[] s = { 'h', 'e', 'l', 'l', 'o' };
21+
new SolutionByTwoPoint().reverseString(s);
22+
System.out.println(s);
23+
}
24+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package algorithm.easy.p937;
2+
3+
import java.util.*;
4+
5+
/**
6+
* https://leetcode.com/problems/reorder-data-in-log-files/
7+
* 2 ms / 38.9 MB
8+
*/
9+
public class Solution {
10+
public String[] reorderLogFiles(String[] logs) {
11+
PriorityQueue<LettersLog> lettersQueue = new PriorityQueue<>(logs.length);
12+
List<String> digitLogs = new ArrayList<>(logs.length);
13+
14+
for (String log : logs) {
15+
// split to identifier, contents
16+
int idx = log.indexOf(' ');
17+
String identifier = log.substring(0, idx);
18+
String contents = log.substring(idx + 1);
19+
20+
// check letters-logs or digit-logs
21+
if (isDigitLogs(contents)) {
22+
digitLogs.add(log);
23+
continue;
24+
}
25+
lettersQueue.offer(new LettersLog(log, identifier, contents));
26+
}
27+
28+
String[] ret = new String[logs.length];
29+
int idx = 0;
30+
while (!lettersQueue.isEmpty()) {
31+
LettersLog log = lettersQueue.poll();
32+
ret[idx++] = log.origin;
33+
}
34+
35+
for (String log : digitLogs) {
36+
ret[idx++] = log;
37+
}
38+
return ret;
39+
}
40+
41+
static boolean isDigitLogs(String log) {
42+
for (int i = 0; i < log.length(); i++) {
43+
char ch = log.charAt(i);
44+
if (ch != ' ' && !Character.isDigit(ch)) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
51+
static class LettersLog implements Comparable<LettersLog> {
52+
String origin;
53+
String identifier;
54+
String contents;
55+
56+
public LettersLog(String origin, String identifier, String contents) {
57+
this.origin = origin;
58+
this.identifier = identifier;
59+
this.contents = contents;
60+
}
61+
62+
@Override
63+
public int compareTo(LettersLog o) {
64+
int cmp = contents.compareTo(o.contents);
65+
if (cmp != 0) {
66+
return cmp;
67+
}
68+
return identifier.compareTo(o.identifier);
69+
}
70+
}
71+
72+
public static void main(String[] args) {
73+
// String[] logs = { "dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero" };
74+
String[] logs = { "a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo" };
75+
String[] results = new Solution().reorderLogFiles(logs);
76+
System.out.println(Arrays.toString(results));
77+
}
78+
}

0 commit comments

Comments
 (0)