Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13e8598

Browse files
solved permutation in string problem
1 parent 9343f31 commit 13e8598

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

‎.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/other.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package java_problem.slidewindow;
2+
3+
4+
import java.util.Arrays;
5+
import java.util.Arrays;
6+
7+
public class PermutationInString1 {
8+
public static void main(String args[]) {
9+
System.out.println(checkInclusion("adc", "dcda"));
10+
}
11+
12+
public static boolean checkInclusion(String s1, String s2) {
13+
int n1 = s1.length();
14+
int n2 = s2.length();
15+
if (n1 > n2) return false;
16+
17+
// Convert s1 to a sorted string
18+
char[] s1Arr = s1.toCharArray();
19+
Arrays.sort(s1Arr);
20+
String nS1 = new String(s1Arr);
21+
22+
// Loop through s2 and check each substring of length n1
23+
for (int i = 0; i <= n2 - n1; i++) {
24+
// Extract the substring from s2 of length n1
25+
String substring = s2.substring(i, i + n1);
26+
27+
// Sort the substring and check if it matches sorted s1
28+
char[] arr = substring.toCharArray();
29+
Arrays.sort(arr);
30+
String sortedSubstring = new String(arr);
31+
32+
if (sortedSubstring.equals(nS1)) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /