Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f6b1829

Browse files
committed
moved initial fork content to backup
1 parent 41a83cf commit f6b1829

File tree

420 files changed

+30360
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

420 files changed

+30360
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author rampatra
7+
* @since 19/11/2018
8+
*/
9+
public class CheckPermutation {
10+
11+
/**
12+
* Checks if {@code s1} is a permutation of {@code s2}.
13+
*
14+
* @param s1
15+
* @param s2
16+
* @return
17+
*/
18+
private static boolean isOnePermutationOfOther(String s1, String s2) {
19+
if (s1.length() != s2.length()) {
20+
return false;
21+
}
22+
23+
char[] c1 = s1.toCharArray();
24+
char[] c2 = s2.toCharArray();
25+
Arrays.sort(c1);
26+
Arrays.sort(c2);
27+
28+
return Arrays.equals(c1, c2);
29+
}
30+
31+
/**
32+
* Checks if {@code s1} is a permutation of {@code s2}.
33+
*
34+
* @param s1
35+
* @param s2
36+
* @return
37+
*/
38+
private static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(String s1, String s2) {
39+
if (s1.length() != s2.length()) {
40+
return false;
41+
}
42+
43+
int[] chars = new int[128]; // assuming strings contain only ASCII characters
44+
45+
for (int i = 0; i < s1.length(); i++) {
46+
chars[s1.charAt(i)]++;
47+
}
48+
49+
for (int i = 0; i < s2.length(); i++) {
50+
chars[s2.charAt(i)]--;
51+
if (chars[s2.charAt(i)] < 0) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
58+
public static void main(String[] args) {
59+
System.out.println(isOnePermutationOfOther("ram", "mar"));
60+
System.out.println(isOnePermutationOfOther("rama", "mar"));
61+
System.out.println(isOnePermutationOfOther("rama", "marA"));
62+
System.out.println("-------");
63+
System.out.println(isOnePermutationOfOtherGivenThatStringsContainOnlyAscii("ram", "mar"));
64+
System.out.println(isOnePermutationOfOtherGivenThatStringsContainOnlyAscii("rama", "mar"));
65+
System.out.println(isOnePermutationOfOtherGivenThatStringsContainOnlyAscii("rama", "marA"));
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 18/11/2018
6+
*/
7+
public class IsUnique {
8+
9+
private static boolean hasAllUniqueCharacters(String str) {
10+
if (str == null || str.length() > 128) return false;
11+
12+
boolean[] charSet = new boolean[128]; // assuming the string contains only ASCII characters
13+
for (int i = 0; i < str.length(); i++) {
14+
int charVal = str.charAt(i);
15+
if (charSet[charVal]) {
16+
return false;
17+
}
18+
charSet[charVal] = true;
19+
}
20+
return true;
21+
}
22+
23+
private static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase(String s) {
24+
int checker = 0;
25+
for (int i = 0; i < s.length(); i++) {
26+
int charValue = s.charAt(i) - 'a';
27+
if ((checker & (1 << charValue)) > 0) {
28+
return false;
29+
}
30+
checker |= (1 << charValue);
31+
}
32+
return true;
33+
}
34+
35+
public static void main(String[] args) {
36+
String s = "ram";
37+
System.out.println(hasAllUniqueCharacters(s));
38+
s = "rama";
39+
System.out.println(hasAllUniqueCharacters(s));
40+
s = "ramA";
41+
System.out.println(hasAllUniqueCharacters(s));
42+
System.out.println("-------");
43+
s = "ram";
44+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
45+
s = "rama";
46+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
47+
// not working as the input contains different cases
48+
s = "ramA";
49+
System.out.println(hasAllUniqueCharactersWhenStringContainsAllLowercase(s));
50+
}
51+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 24/11/2018
6+
*/
7+
public class OneAway {
8+
9+
/**
10+
* Checks if two strings are only one edit away, that is, by inserting, deleting, or editing
11+
* at max one character in {@code s1} it becomes same as {@code s2}.
12+
*
13+
* @param s1
14+
* @param s2
15+
* @return
16+
*/
17+
private static boolean isOneEditAway(String s1, String s2) {
18+
if (s1.length() == s2.length()) {
19+
return isOneCharacterDiffAtMax(s1, s2);
20+
} else if (s1.length() < s2.length()) {
21+
return checkForMaxOneInsertOrDeleteInS1(s1, s2);
22+
} else {
23+
return checkForMaxOneInsertOrDeleteInS1(s1, s2);
24+
}
25+
}
26+
27+
private static boolean isOneCharacterDiffAtMax(String s1, String s2) {
28+
boolean foundDiff = false;
29+
for (int i = 0; i < s1.length(); i++) {
30+
if (s1.charAt(i) != s2.charAt(i)) {
31+
if (foundDiff) {
32+
return false; // means we already found a difference earlier
33+
}
34+
foundDiff = true;
35+
}
36+
}
37+
return true;
38+
}
39+
40+
private static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
41+
int i = 0;
42+
int j = 0;
43+
int s1Len = s1.length();
44+
int s2Len = s2.length();
45+
if (Math.abs(s1Len - s2Len) > 1) return false;
46+
47+
while (i < s1Len && j < s2Len) {
48+
if (s1.charAt(i) != s2.charAt(j)) {
49+
if (s1Len > s2Len) {
50+
i++;
51+
} else {
52+
j++;
53+
}
54+
continue;
55+
}
56+
i++;
57+
j++;
58+
}
59+
return Math.abs(i - j) <= 1; // check whether difference in two strings is not more than 1
60+
}
61+
62+
public static void main(String[] args) {
63+
System.out.println("pale, ple: " + isOneEditAway("pale", "ple"));
64+
System.out.println("pales,pale: " + isOneEditAway("pales", "pale"));
65+
System.out.println("pale, bale: " + isOneEditAway("pale", "bale"));
66+
System.out.println("pale, bake: " + isOneEditAway("pale", "bake"));
67+
System.out.println("ram, rama: " + isOneEditAway("ram", "rama"));
68+
System.out.println("ram, ramaaaaaaa: " + isOneEditAway("ram", "ramaaaaaaa"));
69+
}
70+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author rampatra
8+
* @since 21/11/2018
9+
*/
10+
public class PalindromePermutation {
11+
12+
/**
13+
* This method exploits the fact that a palindrome will contain at most
14+
* one character with odd counts. All other characters should be of even
15+
* counts.
16+
*
17+
* @param str input string
18+
* @return {@code true} if {@code str} is a permutation of a palindrome
19+
*/
20+
private static boolean isPermutationOfPalindrome(String str) {
21+
Map<Character, Integer> charCounts = new HashMap<>();
22+
Integer freq;
23+
int oddCounts = 0; // keep count of odds so that we don't have to loop through the hashmap the second time
24+
for (int i = 0; i < str.length(); i++) {
25+
char c = str.charAt(i);
26+
if (c != 32) {
27+
freq = charCounts.get(c) == null ? 0 : charCounts.get(c);
28+
if ((freq + 1) % 2 == 1) {
29+
oddCounts++;
30+
} else {
31+
oddCounts--;
32+
}
33+
charCounts.put(c, freq + 1);
34+
}
35+
}
36+
return oddCounts <= 1;
37+
}
38+
39+
40+
/**
41+
* This approach sets a bit in a number based on the character in the string and
42+
* then un-sets the bit if it sees the character again. Finally, checks if the
43+
* bitVector has at most one bit set only.
44+
*
45+
* @param str input string
46+
* @return {@code true} if {@code str} is a permutation of a palindrome
47+
*/
48+
private static boolean isPermutationOfPalindromeViaBits(String str) {
49+
int bitVector = 0;
50+
int index;
51+
52+
for (int i = 0; i < str.length(); i++) {
53+
index = getIndex(str.charAt(i));
54+
if (index != -1) {
55+
bitVector = toggleBitAt(bitVector, index);
56+
}
57+
}
58+
return (bitVector & (bitVector - 1)) == 0;
59+
}
60+
61+
/**
62+
* Calculates the index to set the bit according to the character {@code c}.
63+
*
64+
* @param c
65+
* @return the index to set the bit as per the character {@code c}
66+
*/
67+
private static int getIndex(char c) {
68+
char a = 'a';
69+
char z = 'z';
70+
71+
// assuming string contains only lowercase characters
72+
if (c < a || c > z) {
73+
return -1;
74+
}
75+
76+
return c - a;
77+
}
78+
79+
/**
80+
* Toggles the bit at index {@code index} in {@code bitVector}.
81+
*
82+
* @param bitVector
83+
* @param index
84+
* @return the resulting {@code bitVector} after toggling the bit
85+
*/
86+
private static int toggleBitAt(int bitVector, int index) {
87+
return bitVector ^ (1 << index);
88+
}
89+
90+
public static void main(String[] args) {
91+
System.out.println(isPermutationOfPalindrome("tactc oapapa"));
92+
System.out.println(isPermutationOfPalindrome("maam"));
93+
System.out.println(isPermutationOfPalindrome("maa m"));
94+
System.out.println(isPermutationOfPalindrome("rammmar"));
95+
System.out.println(isPermutationOfPalindrome("rammmara"));
96+
System.out.println("---------");
97+
System.out.println(isPermutationOfPalindromeViaBits("tactc oapapa"));
98+
System.out.println(isPermutationOfPalindromeViaBits("maam"));
99+
System.out.println(isPermutationOfPalindromeViaBits("maa m"));
100+
System.out.println(isPermutationOfPalindromeViaBits("rammmar"));
101+
System.out.println(isPermutationOfPalindromeViaBits("rammmara"));
102+
}
103+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-01-20
6+
*/
7+
public class RotateMatrix {
8+
9+
public static void rotateImage(int[][] pixels) {
10+
11+
}
12+
13+
public static void main(String[] args) {
14+
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 24/11/2018
6+
*/
7+
public class StringCompression {
8+
9+
/**
10+
* Compresses the string {@code s} such that a string {@code aabccccaaa} becomes {@code a2b1c4a3}.
11+
* Also, if the compressed string is not shorter than the original, returns the original string.
12+
*
13+
* @param str input string containing only a-z characters, both cases
14+
* @return which ever is the shorter string
15+
*/
16+
private static String compressString(String str) {
17+
StringBuilder compressedSb = new StringBuilder();
18+
int countConsecutive = 0;
19+
for (int i = 0; i < str.length(); i++) {
20+
countConsecutive++;
21+
22+
/* If next character is different than current, append this char to result. */
23+
if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) {
24+
compressedSb.append(str.charAt(i));
25+
compressedSb.append(countConsecutive);
26+
countConsecutive = 0;
27+
}
28+
}
29+
return compressedSb.length() < str.length() ? compressedSb.toString() : str;
30+
}
31+
32+
public static void main(String[] args) {
33+
System.out.println("aabccccaaa: " + compressString("aabccccaaa"));
34+
System.out.println("aabccccAAAA: " + compressString("aabccccAAAA"));
35+
System.out.println("abcd: " + compressString("abcd"));
36+
System.out.println("a: " + compressString("a"));
37+
System.out.println("aabcccccccccccccccccccccccccaaa: " + compressString("aabcccccccccccccccccccccccccaaa"));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.backup.ctci.arraysandstrings;
2+
3+
/**
4+
* Assume you have a method isSubString which checks if one word is a substring of another. Given two
5+
* strings, S1 and S2, write code to check if S2 is a rotation of S1 using only one call to isSubString
6+
* (e.g., "waterbottle" is a rotation of" erbottlewat").
7+
*
8+
* @author rampatra
9+
* @since 2019-01-22
10+
*/
11+
public class StringRotation {
12+
13+
private static boolean isStringRotation(String s1, String s2) {
14+
if (s1.length() != s2.length()) {
15+
return false;
16+
}
17+
s2 = s2 + s2;
18+
return isSubString(s1, s2);
19+
}
20+
21+
/**
22+
* Given method in question.
23+
*
24+
* @param s1 first string
25+
* @param s2 second string
26+
* @return {@code true} if s1 is a substring of s2 or else {@code false}
27+
*/
28+
private static boolean isSubString(String s1, String s2) {
29+
return s2.contains(s1);
30+
}
31+
32+
public static void main(String[] args) {
33+
System.out.println(isStringRotation("waterbottle", "erbottlewat")); // true
34+
System.out.println(isStringRotation("rampatra", "atraramp")); // true
35+
System.out.println(isStringRotation("rampatra", "arampata")); // false
36+
System.out.println(isStringRotation("rampatra", "arampat")); // false
37+
System.out.println(isStringRotation("", "")); // true
38+
}
39+
}

0 commit comments

Comments
 (0)