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

Appearance settings

Commit d2ce2e3

Browse filesBrowse files
authored
Merge pull request #109 from MagicienDeCode/master
add kotlin solution for 567 Permutation-In-String.kt
2 parents aec4966 + 0701afc commit d2ce2e3
Copy full SHA for d2ce2e3

File tree

Expand file treeCollapse file tree

1 file changed

+54
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+54
-0
lines changed
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class PermutationinStringKotlin567 {
2+
fun checkInclusion(s1: String, s2: String): Boolean {
3+
if (s2.isEmpty() || s1.length > s2.length) {
4+
return false
5+
}
6+
val hashArray = IntArray(26)
7+
for (index in s1.indices) {
8+
++hashArray[s1[index] - 'a']
9+
}
10+
var count = 0
11+
var left = 0
12+
for (index in s2.indices) {
13+
if (--hashArray[s2[index] - 'a'] >= 0) {
14+
++count
15+
}
16+
if (index >= s1.length && hashArray[s2[left++] - 'a']++ >= 0) {
17+
--count
18+
}
19+
if (count == s1.length) {
20+
return true
21+
}
22+
}
23+
return false
24+
}
25+
/*
26+
fun checkInclusion(s1: String, s2: String): Boolean {
27+
if (s2.isEmpty() || s1.length > s2.length) {
28+
return false
29+
}
30+
val hashArray = IntArray(26)
31+
for (index in s1.indices) {
32+
++hashArray[s1[index] - 'a']
33+
--hashArray[s2[index] - 'a']
34+
}
35+
if (hashArray.count { it == 0 } == 26) {
36+
return true
37+
}
38+
for (index in 1..s2.length - s1.length) {
39+
++hashArray[s2[index - 1] - 'a']
40+
--hashArray[s2[index + s1.length - 1] - 'a']
41+
if (hashArray.count { it == 0 } == 26) {
42+
return true
43+
}
44+
}
45+
return false
46+
}
47+
*/
48+
}
49+
50+
fun main() {
51+
val solution = PermutationinStringKotlin567()
52+
println(solution.checkInclusion("ab", "eidbaooo")) // true
53+
println(solution.checkInclusion("ab", "eidboaoo")) // false
54+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.