Skip to content

Commit

Permalink
Day 14 - String II
Browse files Browse the repository at this point in the history
  • Loading branch information
xckomorebi committed Aug 30, 2022
1 parent 1050af6 commit d437447
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.vscode/

*.class
174 changes: 173 additions & 1 deletion StringII.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
public class StringII {
public static void main(String[] args) {
StringII s = new StringII();
System.out.println(s.rightShift("abcdefg", 39));
// System.out.println(s.rightShift("abcdefg", 39));
System.out.println(s.replace("123", "12", "4"));
}

/**
Expand Down Expand Up @@ -69,6 +70,54 @@ public String rightShift(String input, int n) {
return new String(ch, n, ch.length - n) + new String(ch, 0, n);
}

/**
* String Replace (basic)
* <p>
* Given an original string input, and two strings S and T, from left to right
* replace all occurrences of S in input with T.
*/
public String replace(String input, String source, String target) {
char[] array = input.toCharArray();
if (source.length() > input.length()) {
return input;
}
if (target.length() > source.length()) {
return replaceLonger(array, source, target);
} else {
return replaceShorter(array, source, target);
}
}

private String replaceShorter(char[] array, String source, String target) {
int i = 0;
int j = 0;
while (i < array.length) {
if (isSubString(array, i, source)) {
i += source.length();
for (int k = 0; k < target.length(); k++) {
array[j++] = target.charAt(k);
}
} else {
array[j++] = array[i++];
}
}
return new String(array, 0, j);
}

private String replaceLonger(char[] array, String source, String target) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < array.length) {
if (isSubString(array, i, source)) {
i += source.length();
sb.append(target);
} else {
sb.append(array[i++]);
}
}
return sb.toString();
}

/**
* All Permutations II
* <p>
Expand All @@ -77,7 +126,130 @@ public String rightShift(String input, int n) {
*/
public List<String> permutations(String input) {
List<String> result = new ArrayList<>();
}


private boolean isSubString(char[] array, int start, String source) {
if (start + source.length() > array.length) {
return false;
}

for (int i = 0; i < source.length(); i++) {
if (array[i + start] != source.charAt(i)) {
return false;
}
}
return true;
}

/**
* ReOrder Array
* <p>
* Given an array of elements, reorder it as follow: * { N1, N2, N3, …, N2k } ->
* { N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k } * { N1, N2, N3, …, N2k+1 } -> {
* N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k, N2k+1 } Try to do it in place
*/
public int[] reorder(int[] array) {
int[] result = new int[array.length];
for (int i = 0; i < array.length / 2; i++) {
result[2 * i] = array[i];
result[2 * i + 1] = array[i + array.length / 2];
}
if (array.length % 2 == 1) {
result[array.length - 1] = array[array.length - 1];
}
return result;
}

/**
* Compress String II
* <p>
* Given a string, replace adjacent, repeated characters with the character
* followed by the number of repeated occurrences.
*/
public String compress(String input) {
char prev = (char) 0;
StringBuilder sb = new StringBuilder();
int cur = 0;
for (char c : input.toCharArray()) {
if (cur == 0) {
prev = c;
cur = 1;
continue;
}
if (prev == c) {
cur++;
} else {
sb.append(prev);
sb.append(cur);
cur = 1;
prev = c;
}
}
if (cur != 0) {
sb.append(prev);
sb.append(cur);
}
return sb.toString();
}

/**
* Decompress String II
* <p>
* Given a string in compressed form, decompress it to the original string. The
* adjacent repeated characters in the original string are compressed to have
* the character followed by the number of repeated occurrences.
*/
public String decompress(String input) {
char[] array = input.toCharArray();
int l = 0;
StringBuilder sb = new StringBuilder();
char prev = (char) 0;

for (char c : array) {
if (c - '0' < 10 && c - '0' >= 0) {
l *= 10;
l += c - '0';
} else {
while (l-- > 0) {
sb.append(prev);
}
l = 0;
prev = c;
}
}
while (l-- > 0) {
sb.append(prev);
}
return sb.toString();
}

/**
* Longest Substring Without Repeating Characters
* <p>
* Given a string, find the longest substring without any repeating characters
* and return the length of it. The input string is guaranteed to be not null.
* For example, the longest substring without repeating letters for "bcdfbd" is
* "bcdf", we should return 4 in this case.
*/
public int longest(String input) {
int max = 0;
boolean[] set = new boolean[26];
int i = 0;
int j = 0;
while (j < input.length()) {
if (!set[input.charAt(j) - 'a']) {
set[input.charAt(j++) - 'a'] = true;
max = Math.max(max, j - i);
} else {
while (input.charAt(i) != input.charAt(j)) {
set[input.charAt(i++) - 'a'] = false;
}
set[input.charAt(i++) - 'a'] = false;
set[input.charAt(j++) - 'a'] = true;
}
}
return max;
}

}

0 comments on commit d437447

Please sign in to comment.