Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions KMP ALGORITHM/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Comfortaa:wght@302&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body >
<h1 id="title">KMP Algorithm - String Matcher</h1>

<label for="inp" class="inp">
<input type="text" name='text' id="text" placeholder="&nbsp">
<span class="label">Enter Pattern</span>
<span class="focus-bg"></span>
</label>
<label for="inp" class="inp">
<input type="text" name='text' id="pattern" placeholder="&nbsp">
<span class="label">Enter Text</span>
<span class="focus-bg"></span>


</label>

<button id="btn" type="button" class="btn btn-outline-dark">Solve !</button>

<div id="output"></div>

<script src="./index.js"></script>

<div id="footer">

<h2 id="h2"> Made By Divyansh Singhal</h2>
</div>

</body>
</html>
70 changes: 70 additions & 0 deletions KMP ALGORITHM/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
let btn = document.getElementById('btn')
let output = document.getElementById('output')

btn.addEventListener("click",()=>{
text = document.getElementById("text").value
pattern = document.getElementById("pattern").value
let res = kmpMatching(text, pattern)
output.innerHTML = `Pattern found at index ${res}`

})

function longestPrefix(str) {
var table = new Array(str.length);
var maxPrefix = 0;

table[0] = 0;
for (var i = 1; i < str.length; i++) {
while (maxPrefix > 0 && str.charAt(i) !== str.charAt(maxPrefix)) {
maxPrefix = table[maxPrefix - 1];
}
if (str.charAt(maxPrefix) === str.charAt(i)) {
maxPrefix++;
}
table[i] = maxPrefix;
}
return table;
}

function kmpMatching(str, pattern) {
var prefixes = longestPrefix(pattern);
var matches = [];
var j = 0;
var i = 0;
while (i < str.length) {
if (str.charAt(i) === pattern.charAt(j)) {
i++;
j++;
}
if (j === pattern.length) {
matches.push(i - j);
j = prefixes[j - 1];
}
else if (str.charAt(i) !== pattern.charAt(j)) {
if (j !== 0) {
j = prefixes[j - 1];
} else {
i++;
}
}
}
return matches;
}


















135 changes: 135 additions & 0 deletions KMP ALGORITHM/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#title{
text-align: center;
color: BLACK;
font-family: 'Comfortaa', cursive;
font-size: 400%;
padding-top: 7%;
}

#btn{
width: 10%;
height: 45%;
margin-left: 45%;
}

body {
height: 100vh;
display: grid;
background-color: black;
font-family: Roboto;
-webkit-text-size-adjust: 100%;
-webkit-font-smoothing: antialiased;
}

* {
box-sizing: border-box;
}



.inp {
position: relative;
margin: auto;
width: 100%;
max-width: 280px;
border-radius: 3px;
overflow: hidden;
}
.inp .label {
position: absolute;
top: 20px;
left: 12px;
font-size: 16px;
font-family: 'Comfortaa', cursive;

font-weight: 500;
transform-origin: 0 0;
transform: translate3d(0, 0, 0);
transition: all 0.2s ease;
pointer-events: none;
}
.inp .focus-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.05);
z-index: -1;
transform: scaleX(0);
transform-origin: left;
}
.inp input {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: 100%;
border: 0;
font-family: 'Comfortaa', cursive;

padding: 16px 12px 0 12px;
height: 56px;
font-size: 16px;
font-weight: 400;

box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.3);
color: black;
transition: all 0.15s ease;
}
.inp input:hover {
background-color: gainsboro;
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.5);
}
.inp input:not(:-moz-placeholder-shown) + .label {

transform: translate3d(0, -12px, 0) scale(0.75);
}
.inp input:not(:-ms-input-placeholder) + .label {

transform: translate3d(0, -12px, 0) scale(0.75);
}
.inp input:not(:placeholder-shown) + .label {

transform: translate3d(0, -12px, 0) scale(0.75);
}
.inp input:focus {

outline: none;
box-shadow: inset 0 -2px 0 #0077FF;
}
.inp input:focus + .label {
color: #0077FF;
transform: translate3d(0, -12px, 0) scale(0.75);
}
.inp input:focus + .label + .focus-bg {
transform: scaleX(1);
transition: all 0.1s ease;
}











#output {
text-align: center;
font-family: 'Comfortaa', cursive;
font-size: 150%;
}

#footer {
background-color: black;
}

#h2 {
color: white;
font-family: 'Comfortaa', cursive;
text-align: center;
padding-top: 2%;

}