-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_09.java
64 lines (60 loc) · 1.52 KB
/
Problem_09.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Cracking-The-Coding-Interview
* Problem_09.java
*/
package com.deepak.ctci.Ch01_Arrays_And_Strings;
/**
* <br> Problem Statement :
*
* Assume you have a method isSubstring which checks
* if one word is a substring of another. Given two
* strings s1 and s2, write code to check if s2 is
* a rotation of s1 using only one call to isSubstring.
*
* ex. "waterbottle" is a rotation of "erbottlewat"
*
* </br>
*
* @author Deepak
*/
public class Problem_09 {
/**
* Method to check if one string is a rotation of another
*
* Time Complexity : O(1)
* Space Complexity : O(1)
*
* @param str1
* @param str2
* @return {@link boolean}
*/
public static boolean isRotation(String str1, String str2) {
/* If any of the string is null, return null */
if (str1 == null || str2 == null) {
return false;
}
/* If one string is rotation of other,
* there length should be equal and greater then zero */
if (str1.length() == str2.length() && str1.length() > 0) {
/* Concatenate str1 and str1 */
String str1str1 = str1.concat(str1);
/* Check if str2 is a substring of new string */
return isSubString(str1str1, str2);
}
return false;
}
/**
* Method to check if a given string is a substring of another
*
* @param big
* @param small
* @return {@link boolean}
*/
private static boolean isSubString(String big, String small) {
if (big.toLowerCase().indexOf(small.toLowerCase()) >= 0) {
/* We can use big.contains(small) as well here */
return true;
}
return false;
}
}