-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface PrefixReport { | ||
[key: string]: { | ||
prefix: string; | ||
array: Array<string>; | ||
}; | ||
} | ||
interface IPrefix { | ||
strs: Array<string>; | ||
isPrefix(): boolean; | ||
findPrefix(): string; | ||
findAllPrefix(): PrefixReport; | ||
} | ||
export default class prefix implements IPrefix { | ||
strs: Array<string>; | ||
constructor(strs: Array<string>); | ||
isPrefix(): boolean; | ||
findPrefix(): string; | ||
findAllPrefix(): PrefixReport; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class prefix { | ||
constructor(strs) { | ||
this.strs = strs; | ||
} | ||
isPrefix() { | ||
let str0 = this.strs[0]; | ||
for (let char of this.strs) { | ||
let i = 0; | ||
while (str0.length > i && char[i] === str0[i]) { | ||
i++; | ||
} | ||
str0 = str0.slice(0, i); | ||
} | ||
return (str0) ? true : false; | ||
} | ||
findPrefix() { | ||
let str0 = this.strs[0]; | ||
for (let char of this.strs) { | ||
let i = 0; | ||
while (str0.length > i && char[i] === str0[i]) { | ||
i++; | ||
} | ||
str0 = str0.slice(0, i); | ||
} | ||
return str0; | ||
} | ||
findAllPrefix() { | ||
let strs = [...this.strs]; | ||
// sort strings by english alphabet | ||
strs.sort((a, b) => a.localeCompare(b)); | ||
let str0 = strs[0]; | ||
let map = {}; | ||
let array = []; | ||
for (let char of strs) { | ||
let i = 0; | ||
if (char[i] !== str0[i]) { | ||
map[str0] = { | ||
prefix: str0, | ||
array: array | ||
}; | ||
array = []; | ||
str0 = char; | ||
array.push(char); | ||
} | ||
else | ||
array.push(char); | ||
while (str0.length > i && char[i] === str0[i]) { | ||
i++; | ||
} | ||
str0 = str0.slice(0, i); | ||
} | ||
map[str0] = { | ||
prefix: str0, | ||
array: array | ||
}; | ||
return map; | ||
} | ||
} | ||
exports.default = prefix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,12 @@ | |
"name": "prefix", | ||
"version": "1.0.0", | ||
"description": "find the prefix of a array of strings", | ||
"main": "index.ts", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "ts-node index.ts", | ||
"build": "tsc index.ts" | ||
"build": "tsc" | ||
}, | ||
"homepage": "https://github.com/alirezakargar1380/prefix-finder.git", | ||
"repository": { | ||
|
@@ -20,9 +21,9 @@ | |
"keywords": [ | ||
"prefix", | ||
"array", | ||
"string" | ||
"string", | ||
"js" | ||
], | ||
"author": "alireza_kargar <[email protected]>", | ||
"license": "ISC" | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters