Skip to content
Open
7 changes: 6 additions & 1 deletion src/main/java/arraystring/_01_01_IsUnique.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package arraystring;

import java.util.Set;
import java.util.stream.Collectors;

/**
* Implement an algorithm to determine if a string has all unique characters.
*/

class _01_01_IsUnique {

boolean isUnique(String str) {
throw new UnsupportedOperationException();
// long distinctCount = str.chars().boxed().distinct().count();
Set<Integer> set = str.chars().boxed().collect(Collectors.toSet());
return set.size() == str.length();
}

}
13 changes: 12 additions & 1 deletion src/main/java/arraystring/_01_02_CheckPermutation.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package arraystring;

import java.util.HashMap;
import java.util.Map;

/**
* Given two strings,write a method to decide if one is a permutation of the other.
*/
class _01_02_CheckPermutation {
boolean check(String a, String b) {
throw new UnsupportedOperationException();
return computeFrequency(a).equals(computeFrequency(b));
}

private Map<Character, Integer> computeFrequency(String s) {
Map<Character, Integer> freq = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
freq.merge(s.charAt(i), 1, (ov, v) -> ov + v);
}
return freq;
}
}
36 changes: 35 additions & 1 deletion src/main/java/arraystring/_01_03_URLify.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package arraystring;

import java.util.Arrays;

/**
* Write a method to replace all spaces in a string with '%20'
* You may assume that the string has sufficient space at the end to hold the additional characters,
Expand All @@ -12,6 +14,38 @@
*/
class _01_03_URLify {
char[] urlify(char[] chars, int trueLength) {
throw new UnsupportedOperationException();
int spaces = 0;
for (int i = 0; i < trueLength; i++) {
if (chars[i] == ' ') {
spaces++;
}
}
if ((chars.length - trueLength) != (2 * spaces)) {
throw new IllegalArgumentException("incorrect length " + chars.length + ": " + (trueLength + 2 * spaces) + " required.");
}
/* this my own way is not efficient, the master one is better
int j = trueLength;
for (int i = trueLength; i > 0; i--) {
if (chars[i-1] == ' ') {
char[] tmp = Arrays.copyOfRange(chars, i, j);
chars[i-1] = '%';
chars[i] = '2';
chars[i+1] = '0';
System.arraycopy(tmp, 0, chars, i+2, tmp.length);
j += 2;
}
}
*/
for (int i = trueLength - 1, j = chars.length - 1; i >= 0; i--) {
if (chars[i] == ' ') {
chars[j--] = '0';
chars[j--] = '2';
chars[j--] = '%';
}
else {
chars[j--] = chars[i];
}
}
return chars;
}
}
18 changes: 17 additions & 1 deletion src/main/java/arraystring/_01_04_PalindromePermutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
*/
class _01_04_PalindromePermutation {
boolean check(String s) {
throw new UnsupportedOperationException();
char[] chars = s.toCharArray();
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j && !Character.isLetterOrDigit(chars[i])) {
i++;
}
while (j > i && !Character.isLetterOrDigit(chars[j])) {
j--;
}
if (i >= j) return true;
if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) {
return false;
}
i++;
j--;
}
return true;
}
}
30 changes: 29 additions & 1 deletion src/main/java/arraystring/_01_05_OneAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,35 @@
*/
class _01_05_OneAway {
boolean isOneAway(String a, String b) {
throw new UnsupportedOperationException();
int len = a.length();
if (Math.abs(a.length() - b.length()) == 1) {
char[] shorter = a.length() < b.length() ? a.toCharArray() : b.toCharArray();
char[] longer = a.length() < b.length() ? b.toCharArray() : a.toCharArray();
for (int i = 0, j = 0; i < shorter.length; i++) {
while (shorter[i] != longer[j++]) {
if (j > i+1) {
return false;
}
}
}
}
else if (a.length() == b.length()) {
boolean hasDifferent = false;
for (int i = 0; i < a.length(); i ++) {
if (a.charAt(i) != b.charAt(i)) {
if (!hasDifferent) {
hasDifferent = true;
}
else {
return false;
}
}
}
}
else {
return false;
}
return true;
}

}
142 changes: 141 additions & 1 deletion src/main/java/arraystring/_01_06_StringCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,146 @@
*/
class _01_06_StringCompression {
String compress(String s) {
throw new UnsupportedOperationException();
StringBuilder sb = new StringBuilder();
char c = ' ';
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != c) {
if (count > 0) {
sb.append(c).append(count);
}
c = s.charAt(i);
count = 1;
}
else {
count++;
}
}
if (count > 0) {
sb.append(c).append(count);
}
return sb.length() < s.length() ? sb.toString() : s;
}

/**
* Given an array of characters, compress it in-place.
*
* The length after compression must always be smaller than or equal to the original array.
*
* Every element of the array should be a character (not int) of length 1.
*
* After you are done modifying the input array in-place, return the new length of the array.
*
* Example 1:
*
* Input:
* ["a","a","b","b","c","c","c"]
*
* Output:
* Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
*
* Explanation:
* "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
*
* Example 2:
*
* Input:
* ["a"]
*
* Output:
* Return 1, and the first 1 characters of the input array should be: ["a"]
*
* Explanation:
* Nothing is replaced.
*
* Example 3:
*
* Input:
* ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
*
* Output:
* Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
*
* Explanation:
* Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
*
* @param chars
* @return
*/
public int compress_leetcode_443(char[] chars) {
if (chars.length <= 1) return chars.length;
char c = chars[0];
int count = 0;
int j = 0;
for (int i = 1; i <= chars.length; i++) {
if (i < chars.length && chars[i] == c) { // repeating characters
count++;
}
else if (i >= chars.length || c != chars[i]) { // to the end or encounter a new character
chars[j++] = c;
if (count > 0) {
String s = String.valueOf(count + 1);
System.arraycopy(s.toCharArray(), 0, chars, j, s.length());
j += s.length();
}
if (i < chars.length) {
c = chars[i];
count = 0;
}
}
}
return j;
}

public int compress_leetcode_443_2nd(char[] chars) {
if (chars.length <= 1) return chars.length;
char c = chars[0];
int count = 0;
int j = 0;
for (int i = 1; i < chars.length; i++) {
if (chars[i] == c) { // count repeating characters
count++;
}
if (i + 1 == chars.length || c != chars[i]) { // to the end of the chars or a different character
chars[j++] = c; // start to output last character
if (count > 0) { // append count of repeating times to the character
String s = String.valueOf(count + 1);
System.arraycopy(s.toCharArray(), 0, chars, j, s.length());
j += s.length();
}
if (i + 1 == chars.length && chars[i] != c) {
chars[j++] = chars[i];
}
c = chars[i];
count = 0;
}
}
return j;
}

public int compress_leetcode_443_best(char[] chars) {
int anchor = 0, write = 0;
for (int read = 0; read < chars.length; read++) {
if ((read + 1 == chars.length) || (chars[read + 1] != chars[read])) {
chars[write++] = chars[anchor];
if (read > anchor) {
for (char c : Integer.toString(read - anchor + 1).toCharArray()) {
chars[write++] = c;
}
}
anchor = read + 1;
}
}
return write;
}

public static void main(String args[]) {
_01_06_StringCompression solution = new _01_06_StringCompression();
char[] chars = "aabbcccd".toCharArray();
int len = solution.compress_leetcode_443(chars);
System.out.println("After compress_mine: " + new String(chars).substring(0, len));
chars = "aabbcccd".toCharArray();
len = solution.compress_leetcode_443_best(chars);
System.out.println("After compress_best: " + new String(chars).substring(0, len));
}
}
58 changes: 57 additions & 1 deletion src/main/java/arraystring/_01_07_RotateMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,67 @@
/**
* Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes,
* write a method to rotate the image by 90 degrees. Can you do this in place?
*
* refer to leetcode 48
*/
class _01_07_RotateMatrix {
int[][] rotate(int[][] m) {
throw new UnsupportedOperationException();
int start = 0, end = m.length - 1;
while (start < end) { // rotate from outside to inside, each rotation is a level
rotateBoundary(m, start, end);
start++;
end--;
}
return m;
}

/**
* rotate the ring area of the matrix bounded by start and end
* @param m the matrix for rotation
* @param start the start index of the boundary
* @param end the end index of the boundary
*/
private void rotateBoundary(int[][] m, int start, int end) {
if (start >= end) {
}
for (int i = start; i < end; i++) {
int x = i, y = start;
int last = m[x][y];
for (int j = 0; j < 4; j++) {
// new coordinate:
// x' = y
// y' = n - x, here n = matrix size minus 1
int tmp = x;
x = y;
y = m.length - 1 - tmp;
tmp = m[x][y];
m[x][y] = last;
last = tmp;
}
}
}

int[][] rotate_better(int[][] m) {
swapRow(m);
transpose(m);
return m;
}

private void swapRow(int[][] m) {
for (int start = 0, end = m.length - 1; start < end; ++start, ++end) {
int[] tmp = m[start];
m[start] = m[end];
m[end] = tmp;
}
}

private void transpose(int[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = i + 1; j < m.length; j++) {
int tmp = m[i][j];
m[i][j] = m[j][i];
m[j][i] = tmp;
}
}
}
}
Loading