From d4374476b6e301a593b3f650d020d041d48a6ce7 Mon Sep 17 00:00:00 2001 From: Chen Xu Date: Tue, 30 Aug 2022 04:15:58 -0400 Subject: [PATCH] Day 14 - String II --- .gitignore | 2 + StringII.java | 174 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 175 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1d74e21..f1341ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .vscode/ + +*.class diff --git a/StringII.java b/StringII.java index b81bc56..6bb5301 100644 --- a/StringII.java +++ b/StringII.java @@ -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")); } /** @@ -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) + *

+ * 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 *

@@ -77,7 +126,130 @@ public String rightShift(String input, int n) { */ public List permutations(String input) { List 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 + *

+ * 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 + *

+ * 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 + *

+ * 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 + *

+ * 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; + } + }