File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Filter options
May-LeetCoding-Challenge/18-Permutation-In-String Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments