Skip to content

Commit 3a27e56

Browse files
committed
String Rotation done plus some clean up
1 parent 23024c2 commit 3a27e56

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.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+
}

src/main/java/com/rampatra/linkedlists/LRUCache.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ E get(E item) {
4949
return itemFromCache;
5050
}
5151

52-
void printCache() {
52+
private void printCache() {
5353
Iterator<E> iterator = linkedHashMap.keySet().iterator();
5454
while (iterator.hasNext()) {
5555
System.out.print(iterator.next() + ((iterator.hasNext()) ? "," : "\n"));
5656
}
5757
}
5858

59-
public static void main(String a[]) {
59+
public static void main(String[] a) {
6060
LRUCache<Integer> cache = new LRUCache<>(3);
6161
cache.add(1);
6262
cache.add(2);

src/main/java/com/rampatra/linkedlists/MaximumSumLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> maximumSumLinkedList
7575
return head;
7676
}
7777

78-
public static void main(String a[]) {
78+
public static void main(String[] a) {
7979
SingleLinkedList<Integer> linkedList1 = new SingleLinkedList<>();
8080
linkedList1.add(00);
8181
linkedList1.add(11);

src/main/java/com/rampatra/linkedlists/MergeSort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> mergeTwoSortedLists(
111111
return head.next;
112112
}
113113

114-
public static void main(String a[]) {
114+
public static void main(String[] a) {
115115
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
116116
linkedList.add(21);
117117
linkedList.add(33);

0 commit comments

Comments
 (0)