forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoyer_Moore.kt
More file actions
78 lines (66 loc) · 2.37 KB
/
Boyer_Moore.kt
File metadata and controls
78 lines (66 loc) · 2.37 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Boyer Moore String Matching Algorithm in Kotlin
Author: Arkadip Bhattacharya (@darkmatter18)
*/
import java.util.*
internal object Main {
private var NO_OF_CHARACTERS = 256
private fun maxOfAAndB(a: Int, b: Int): Int {
return a.coerceAtLeast(b)
}
private fun badCharacterHeuristic(str: CharArray, size: Int, badcharacter: IntArray) {
// Initialize all elements of bad character as -1
var i = 0
while (i < NO_OF_CHARACTERS) {
badcharacter[i] = -1
i++
}
// Fill the actual value of last occurrence of a character
i = 0
while (i < size) {
badcharacter[str[i].toInt()] = i
i++
}
}
// A pattern searching function
private fun search(text: CharArray, pattern: CharArray) {
val m = pattern.size
val n = text.size
val badCharacterHeuristic = IntArray(NO_OF_CHARACTERS)
// Fill the bad character array by calling the preprocessing function
badCharacterHeuristic(pattern, m, badCharacterHeuristic)
// s is shift of the pattern with respect to text
var s = 0
while (s <= n - m) {
var j = m - 1
// Keep reducing index j of pattern while characters of pattern and text are matching at this shift s
while (j >= 0 && pattern[j] == text[s + j]) j--
// If the pattern is present at current shift, then index j will become -1 after the above loop
s += if (j < 0) {
println("Patterns occur at shift = $s")
// Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern.
// The condition s+m < n is necessary for the case when pattern occurs at the end of text
if (s + m < n) m - badCharacterHeuristic[text[s + m].toInt()] else 1
} else maxOfAAndB(1, j - badCharacterHeuristic[text[s + j].toInt()])
}
}
/* Driver program*/
@JvmStatic
fun main(args: Array<String>) {
val sc = Scanner(System.`in`)
var s = sc.nextLine()
val text = s.toCharArray()
s = sc.nextLine()
val pattern = s.toCharArray()
search(text, pattern)
}
}
/*
Sample Input:
AABAACAADAABAABA
AABA
Sample Output:
Patterns occur at shift = 0
Patterns occur at shift = 9
Patterns occur at shift = 12
*/