【Lintcode】318. Character Grid
題目地址:
https://www.lintcode.com/problem/character-grid/description
給定兩個字串
A
A
A和
B
B
B,返回一個矩陣。串
A
A
A要求從左往右輸出,串
B
B
B要求從上往下輸出,兩串交於一點(即有一個字母共用)。共用的字母要求是串A中第一個在串B也包含的字母,並且要求交點是兩串中各自第一次出現共用的字母的位置。別的位置填.
補齊。
先從 A A A中找到第一個包含在 B B B中的字元,然後構造矩陣。程式碼如下:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Solution {
/**
* @param A: A string
* @param B: A string
* @return: A string array
*/
public List<String> characterGrid(String A, String B) {
// write your code here.
List<String> res = new ArrayList<>();
Set<Character> set = new HashSet<>();
for (int i = 0; i < B.length(); i++) {
set.add(B.charAt(i));
}
int posA = -1;
for (int i = 0; i < A.length(); i++) {
if (set.contains(A.charAt(i))) {
posA = i;
break;
}
}
int posB = B.indexOf(A.charAt(posA));
for (int i = 0; i < B.length(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < A.length(); j++) {
if (j != posA) {
sb.append('.');
} else {
sb.append(B.charAt(i));
}
}
res.add(sb.toString());
}
res.set(posB, A);
return res;
}
}
時空複雜度 O ( l A l B ) O(l_Al_B) O(lAlB)。
相關文章
- 常見物件-Character物件
- Swift 字元(Character)講解Swift字元
- python character stringPython
- java lang包下Character類Java
- Hive throws: WstxParsingException: Illegal character entity: expansion character (code 0x8)HiveException
- Unknown initial character set index ‘255‘ received from server. Initial client character set can beIndexServerclient
- java學習值Character類Java
- [LintCode] Daily TemperaturesAI
- LintCode 子樹
- LintCode-Backpack
- LintCode-HeapifyAPI
- grid
- Would you like to develop a story for your character?dev
- Caused by: Error: ' ' is not a valid resource name characterError
- Indexing Mixed-Character Set ColumnsIndex
- character_set_database的意義Database
- using escape character whitin oracleOracle
- 【Mysql】character-set-server引數MySqlServer
- Using Byte and Char with Character Datatypes
- [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 字串比較字串
- Grid Points
- [原創] How to show chinese character in Git StatusGit
- Java裡的Character類的基本用法Java