forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringPermutationsCount.java
41 lines (36 loc) · 1.19 KB
/
StringPermutationsCount.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
package com.rampatra.permutations;
/**
* Created by IntelliJ IDEA.
* <p/>
* You have 2 string, one smaller, one larger. Write an algorithm to figure out how many permutations of the
* smaller string exist in the bigger string.
*
* @author rampatra
* @since 10/15/15
* @time: 10:32 AM
* @see: StringPermutations for a simpler version
*/
public class StringPermutationsCount {
/**
* Finds the number of permutations of string {@param s1} that exists in string {@param s2}.
*
* @param prefix
* @param s1
* @param s2
* @param count
* @return
*/
public static int getStringPermutationsCount(String prefix, String s1, String s2, int count) {
if (s1.isEmpty()) {
if (s2.indexOf(prefix) != -1) count++;
}
for (int i = 0; i < s1.length(); i++) {
count = getStringPermutationsCount(prefix + s1.substring(i, i + 1), s1.substring(0, i) + s1.substring(i + 1), s2, count);
}
return count;
}
public static void main(String[] args) {
System.out.println(getStringPermutationsCount("", "abc", "abcba", 0));
System.out.println(getStringPermutationsCount("", "abc", "abcbacb", 0));
}
}