[LeetCode] 784. Letter Case Permutation

Given a string s, we can transform every letter individually to be lowercase or uppercase to create another string.

Return a list of all possible strings we could create. You can return the output in any order.

Example 1:

Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]

Example 2:

Input: s = "3z4"
Output: ["3z4","3Z4"]

Example 3:

Input: s = "12345"
Output: ["12345"]

Example 4:

Input: s = "0"
Output: ["0"]

Constraints:

  • s will be a string with length between 1 and 12.
  • s will consist only of letters or digits.

字母大小写全排列。

题意是给一个字符串,请你返回他的大小写全排列,意思是每当遇到其中的字母的时候,需要大写字母有一份,小写字母也有一份。

我这里提供三种做法,一种就是简单的遍历,一种 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 经典的模板的基础上稍作变化。遇到数字就直接递归到下一层,遇到字母的时候需要用大小写分别递归到下一层。

时间O(2^n * n) - n是字符串的长度

空间O(2^n * n)

Java实现

 1 class Solution {
 2     List<String> letterCasePermutation(String s) {
 3         List<String> res = new ArrayList<>();
 4         // corner case
 5         if (s == null || s.length() == 0) {
 6             return res;
 7         }
 8 
 9         // normal case
10         helper(res, 0, s.toCharArray());
11         return res;
12     }
13 
14     private void helper(List<String> res, int index, char[] s) {
15         // base case
16         if (index == s.length) {
17             res.add(new String(s));
18             return;
19         }
20         if (Character.isLetter(s[index])) {
21             s[index] = Character.toUpperCase(s[index]);
22             helper(res, index + 1, s);
23             s[index] = Character.toLowerCase(s[index]);
24             helper(res, index + 1, s);
25         } else {
26             helper(res, index + 1, s);
27         }
28     }
29 }

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 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13277173.html