【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是最長字串長度。
相關文章
- The password supplied with the username Domain\UserName was not correct. Verify that it was entered ...AI
- URL username 屬性
- su - username -c執行多條命令
- WCF Security:authentication based on Username/Password - Part I
- 每天閱讀一個 npm 模組(1)- usernameNPM
- [LintCode] Daily TemperaturesAI
- LintCode 子樹
- LintCode-Backpack
- LintCode-HeapifyAPI
- 關於根據USERNAME取密碼的問題密碼
- [LintCode] Permutation in String
- LintCode 主元素 II
- LintCode 解碼方法
- LintCode-Search for a Range
- LintCode-K Sum
- LintCode-Word SegmentationSegmentation
- LintCode-Hash FunctionFunction
- LintCode-Fast PowerAST
- Lintcode-Max Tree
- LintCode-Partition Array
- LintCode-Subarray Sum
- LintCode-Majority Number
- LintCode-A+B Problem
- LintCode-BackPack II
- LintCode-Previous Permuation
- LintCode 字串比較字串
- python urllib socks5 auth username password 設定Python
- WCF Services Sample: Authenticate Silverlight Client based on UserName and Passwordclient
- [LintCode] 3Sum Smaller
- 【Lintcode】572. Music PairsAI
- 【Lintcode】576. Split Array
- 【Lintcode】1736. Throw Garbage
- LintCode - A + B 問題(普通)
- Lintcode 反轉整數
- 表示式展開-LintCode
- LintCode 最大正方形
- LintCode 奇偶分割陣列陣列
- LintCode 搜尋插入位置