1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
9286.FirstUniqueCharIndex.ts 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-04-28 17:23 . 提交更新
/*
9286 - FirstUniqueCharIndex
-------
by jiangshan (@jiangshanmeta) #medium #string
### Question
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. (Inspired by [leetcode 387](https://leetcode.com/problems/first-unique-character-in-a-string/))
> View on GitHub: https://tsch.js.org/9286
*/
/* _____________ Your Code Here _____________ */
type OneByString<T extends string, U extends string, i extends number[] = []> = T extends `${infer S}${infer R}` ? S extends U ? OneByString<R, U, [...i, 0]> : OneByString<R, U, i> : i['length']
type FirstUniqueCharIndex<T extends string,N extends string = T ,i extends string[] = []> = N extends `${infer S}${infer R}` ? OneByString<T, S> extends 1
? i['length'] : FirstUniqueCharIndex<T,R,[...i, S]> : -1
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<FirstUniqueCharIndex<'leetcode'>, 0>>,
Expect<Equal<FirstUniqueCharIndex<'loveleetcode'>, 2>>,
Expect<Equal<FirstUniqueCharIndex<'aabb'>, -1>>,
Expect<Equal<FirstUniqueCharIndex<''>, -1>>,
Expect<Equal<FirstUniqueCharIndex<'aaa'>, -1>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/9286/answer
> View solutions: https://tsch.js.org/9286/solutions
> More Challenges: https://tsch.js.org
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wang-zichu/ts-challenges.git
git@gitee.com:wang-zichu/ts-challenges.git
wang-zichu
ts-challenges
ts-challenges
master

搜索帮助