[LeetCode] 784. Letter Case Permutation
Given a string s
, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example 1:
Input: s = "a1b2" Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: s = "3z4" Output: ["3z4","3Z4"]
Constraints:
1 <= s.length <= 12
s
consists of lowercase English letters, uppercase English letters, and digits.
字母大小写全排列。
给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。
返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/letter-case-permutation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是给一个字符串,请你返回他的大小写全排列,意思是每当遇到其中的字母的时候,需要大写字母有一份,小写字母也有一份。
我这里提供三种做法,一种就是简单的遍历,一种 DFS,一种 BFS。
首先是遍历。开始遍历 input 字符串,并且创建一个 list of StringBuilder,当遇到数字的时候就直接将数字加入每一个 StringBuilder;当遇到字母的时候,需要遍历之前创建过的每一个 StringBuilder,并将当前遍历到的字母,大写 copy 一份,小写也 copy 一份。
时间O(2^n * n) - n是字符串的长度
空间O(2^n * n)
Java实现
1 class Solution { 2 public List<String> letterCasePermutation(String S) { 3 List<StringBuilder> ans = new ArrayList<>(); 4 ans.add(new StringBuilder()); 5 for (char c : S.toCharArray()) { 6 int n = ans.size(); 7 if (Character.isLetter(c)) { 8 for (int i = 0; i < n; i++) { 9 ans.add(new StringBuilder(ans.get(i))); 10 ans.get(i).append(Character.toLowerCase(c)); 11 ans.get(n + i).append(Character.toUpperCase(c)); 12 } 13 } else { 14 for (int i = 0; i < n; i++) { 15 ans.get(i).append(c); 16 } 17 } 18 } 19 List<String> res = new ArrayList<>(); 20 for (StringBuilder sb : ans) { 21 res.add(sb.toString()); 22 } 23 return res; 24 } 25 }
DFS/backtracking。会在 backtracking 经典的模板的基础上稍作变化。遇到数字就直接递归到下一层,遇到字母的时候需要用大小写分别递归到下一层。
时间O(2^n * n) - n是字符串的长度
空间O(2^n * n)
Java实现
1 class Solution { 2 public List<String> letterCasePermutation(String s) { 3 List<String> res = new ArrayList<>(); 4 helper(res, 0, s.toCharArray()); 5 return res; 6 } 7 8 private void helper(List<String> res, int index, char[] letters) { 9 if (index == letters.length) { 10 res.add(new String(letters)); 11 return; 12 } 13 if (Character.isLetter(letters[index])) { 14 letters[index] = Character.toUpperCase(letters[index]); 15 helper(res, index + 1, letters); 16 letters[index] = Character.toLowerCase(letters[index]); 17 helper(res, index + 1, letters); 18 } else { 19 helper(res, index + 1, letters); 20 } 21 } 22 }
BFS
时间O(2^n * n) - n是字符串的长度
空间O(n) - queue
Java实现
1 class Solution { 2 public List<String> letterCasePermutation(String S) { 3 // corner case 4 if (S == null || S.length() == 0) { 5 return new LinkedList<>(); 6 } 7 8 // normal case 9 Queue<String> queue = new LinkedList<>(); 10 queue.offer(S); 11 for (int i = 0; i < S.length(); i++) { 12 if (Character.isDigit(S.charAt(i))) { 13 continue; 14 } 15 int size = queue.size(); 16 for (int j = 0; j < size; j++) { 17 String cur = queue.poll(); 18 char[] chs = cur.toCharArray(); 19 20 chs[i] = Character.toUpperCase(chs[i]); 21 queue.offer(String.valueOf(chs)); 22 23 chs[i] = Character.toLowerCase(chs[i]); 24 queue.offer(String.valueOf(chs)); 25 } 26 } 27 // return new LinkedList<>(queue); 28 List<String> res = new ArrayList<>(); 29 while (!queue.isEmpty()) { 30 res.add(queue.poll()); 31 } 32 return res; 33 } 34 }