-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3412 (#3930)
No.3412.Find Mirror Score of a String
- Loading branch information
Showing
7 changed files
with
319 additions
and
8 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
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
24 changes: 24 additions & 0 deletions
24
solution/3400-3499/3412.Find Mirror Score of a String/Solution.cpp
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,24 @@ | ||
class Solution { | ||
public: | ||
long long calculateScore(string s) { | ||
unordered_map<char, vector<int>> d; | ||
int n = s.length(); | ||
long long ans = 0; | ||
for (int i = 0; i < n; ++i) { | ||
char x = s[i]; | ||
char y = 'a' + 'z' - x; | ||
if (d.contains(y)) { | ||
vector<int>& ls = d[y]; | ||
int j = ls.back(); | ||
ls.pop_back(); | ||
if (ls.empty()) { | ||
d.erase(y); | ||
} | ||
ans += i - j; | ||
} else { | ||
d[x].push_back(i); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
solution/3400-3499/3412.Find Mirror Score of a String/Solution.go
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,17 @@ | ||
func calculateScore(s string) (ans int64) { | ||
d := make(map[rune][]int) | ||
for i, x := range s { | ||
y := 'a' + 'z' - x | ||
if ls, ok := d[y]; ok { | ||
j := ls[len(ls)-1] | ||
d[y] = ls[:len(ls)-1] | ||
if len(d[y]) == 0 { | ||
delete(d, y) | ||
} | ||
ans += int64(i - j) | ||
} else { | ||
d[x] = append(d[x], i) | ||
} | ||
} | ||
return | ||
} |
22 changes: 22 additions & 0 deletions
22
solution/3400-3499/3412.Find Mirror Score of a String/Solution.java
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,22 @@ | ||
class Solution { | ||
public long calculateScore(String s) { | ||
Map<Character, List<Integer>> d = new HashMap<>(26); | ||
int n = s.length(); | ||
long ans = 0; | ||
for (int i = 0; i < n; ++i) { | ||
char x = s.charAt(i); | ||
char y = (char) ('a' + 'z' - x); | ||
if (d.containsKey(y)) { | ||
var ls = d.get(y); | ||
int j = ls.remove(ls.size() - 1); | ||
if (ls.isEmpty()) { | ||
d.remove(y); | ||
} | ||
ans += i - j; | ||
} else { | ||
d.computeIfAbsent(x, k -> new ArrayList<>()).add(i); | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
solution/3400-3499/3412.Find Mirror Score of a String/Solution.py
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,12 @@ | ||
class Solution: | ||
def calculateScore(self, s: str) -> int: | ||
d = defaultdict(list) | ||
ans = 0 | ||
for i, x in enumerate(s): | ||
y = chr(ord("a") + ord("z") - ord(x)) | ||
if d[y]: | ||
j = d[y].pop() | ||
ans += i - j | ||
else: | ||
d[x].append(i) | ||
return ans |
24 changes: 24 additions & 0 deletions
24
solution/3400-3499/3412.Find Mirror Score of a String/Solution.ts
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,24 @@ | ||
function calculateScore(s: string): number { | ||
const d: Map<string, number[]> = new Map(); | ||
const n = s.length; | ||
let ans = 0; | ||
for (let i = 0; i < n; i++) { | ||
const x = s[i]; | ||
const y = String.fromCharCode('a'.charCodeAt(0) + 'z'.charCodeAt(0) - x.charCodeAt(0)); | ||
|
||
if (d.has(y)) { | ||
const ls = d.get(y)!; | ||
const j = ls.pop()!; | ||
if (ls.length === 0) { | ||
d.delete(y); | ||
} | ||
ans += i - j; | ||
} else { | ||
if (!d.has(x)) { | ||
d.set(x, []); | ||
} | ||
d.get(x)!.push(i); | ||
} | ||
} | ||
return ans; | ||
} |