【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)。
相關文章
- python character stringPython
- CSS content character ACSS
- Character Specifications for A Word
- grid
- Hive throws: WstxParsingException: Illegal character entity: expansion character (code 0x8)HiveException
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- RAC and Grid
- Grid Points
- Swift 字元(Character)講解Swift字元
- Unknown initial character set index ‘255‘ received from server. Initial client character set can beIndexServerclient
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- export GRID_HOME=/u01/app/19c/grid $GRID_HOME/bin/crExportAPP
- grid佈局
- Shichikuji and Power Grid
- Caused by: Error: ' ' is not a valid resource name characterError
- LeetCode之Shortest Distance to a Character(Kotlin)LeetCodeKotlin
- [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】1789. Distinguish UsernameNGUI
- 【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