【Lintcode】1789. Distinguish Username
題目地址:
https://www.lintcode.com/problem/distinguish-username/description
給定一個字串陣列 A A A,要求返回一個同長的陣列 B B B,使得 B [ i ] B[i] B[i]是 A [ i ] A[i] A[i]拼接上其前面的子陣列裡其出現的次數。次數是 0 0 0則省略。
思路是雜湊表,記錄每個字串出現次數。程式碼如下:
import java.util.HashMap;
import java.util.Map;
public class Solution {
/**
* @param names: a string array
* @return: the string array
*/
public String[] DistinguishUsername(String[] names) {
// Write your code here
String[] res = new String[names.length];
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < names.length; i++) {
map.put(names[i], map.getOrDefault(names[i], 0) + 1);
int val = map.get(names[i]);
res[i] = val > 1 ? names[i] + (val - 1) : names[i];
}
return res;
}
}
時間複雜度 O ( n l ) O(nl) O(nl), n n n是陣列長度, l l l是最長字串長度。
相關文章
- URL username 屬性
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- Hexo 部落格部署失敗 could not read UsernameHexo
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- 每天閱讀一個 npm 模組(1)- usernameNPM
- python urllib socks5 auth username password 設定Python
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- [LintCode] Check Full Binary Tree
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- [LintCode] 3Sum Smaller
- 【Lintcode】1615. The Result of Investment
- [LintCode] Binary Tree Level Order
- 【Lintcode】1736. Throw Garbage
- 【Lintcode】1665. Calculate Number
- 【Lintcode】1562. Number of RestaurantsREST
- 【Lintcode】576. Split Array
- 【Lintcode】1267. Lexicographical Numbers
- 【Lintcode】141. Sqrt(x)
- 【Lintcode】1415. Residual Product
- 【Lintcode】1230. Assign CookiesCookie
- 【Lintcode】1732. Snakes and Ladders
- 【Lintcode】1218. Number Complement
- 【Lintcode】1850. Pick ApplesAPP
- 【Lintcode】572. Music PairsAI
- 【Lintcode】318. Character Grid
- 【Lintcode】1891. Travel Plan
- [LintCode/LeetCode] Check Sum of K PrimesLeetCode
- [LintCode]NumberofIslands(島嶼個數)
- lintcode-514-柵欄染色
- 【Lintcode】1322. Product Equal B
- 【Lintcode】191. Maximum Product Subarray
- 【Lintcode】1786. Pub Sub Pattern
- 【Lintcode】1793. Balanced Sales Array
- 【Lintcode】1623. Minimal Distance In The Array