Skip to content

Instantly share code, notes, and snippets.

@kayac-chang
Created July 5, 2023 09:02
Show Gist options
  • Save kayac-chang/c8e7840bc56b691ef8db176d2f1b6d93 to your computer and use it in GitHub Desktop.
Save kayac-chang/c8e7840bc56b691ef8db176d2f1b6d93 to your computer and use it in GitHub Desktop.
Find name contain most words
import { test, expect } from "vitest";
function isLowercase(char: string): boolean {
return char >= "a" && char <= "z";
}
function isUppercase(char: string): boolean {
return char >= "A" && char <= "Z";
}
function countWords(name: string): number {
let isAbbr = false;
let count = 0;
for (const char of name) {
if (isLowercase(char)) {
isAbbr = false;
}
if (!isAbbr && isUppercase(char)) {
count += 1;
isAbbr = true;
}
}
return count;
}
function findNameContainMostWords(dict: string[]): string {
let maxCount = -1;
let name = "";
for (let i = 0; i < dict.length; i++) {
// count number of word
const count = countWords(dict[i]!);
if (maxCount < count) {
maxCount = count;
name = dict[i]!;
}
}
return name;
}
test("should return HelloWorldWideWeb", () => {
expect(
findNameContainMostWords([
"Hi",
"Hello",
"HelloWorld",
"HiWorld",
"HelloWorldWideWeb",
"HelloWWW",
])
).toBe("HelloWorldWideWeb");
});
test('should return "SkymakersDigitalLTD"', () => {
expect(
findNameContainMostWords([
"Oursky",
"OurSky",
"OurskyLimited",
"OurskyHK",
"SkymakersDigitalLTD",
"Skymake",
])
).toBe("SkymakersDigitalLTD");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment