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. Examples:
Input: S = “a1b2”
Output: [“a1b2”, “a1B2”, “A1b2”, “A1B2”]
Input: S = “3z4”
Output: [“3z4”, “3Z4”]
Input: S = “12345”
Output: [“12345”]
Note: S will be a string with length at most 12. S will consist only of letters or digits.
DFS (Depth First Search) using Recursion
Starting from the first letter, we recursively concat it. Each letter could have maximum two variants (lowercase and uppercase). When we reach the end of the string, we push the current string to the result vector.
DFS can be implemented using a helper function in recursion.
1 | class Solution { |
Reposted to my blog for better indexing.
Support me and my work as a witness - witness thread by
Thank you! Some of My Contributions: SteemIt Tutorials, Robots, Tools and APIs
题意:给定一个字符串,输出所有字符大小写都可以组成的字符串。
如: “ab1” 能成生 [“ab1”, “Ab1”, “aB1”, “AB1”]
DFS 深度优先 - 递归
我们可以从字符串的开头递归的把当前字符给添加到最终的字符串中,当当前字符是字母的时候,就有两种可能了。当到达字符串尾部的时候我们把当前字符串添加到结果数组中即可。
1 | class Solution { |
刚刚同步到我的博客:https://justyy.com/archives/6445
支持我的工作 支持我成为 见证人
- 请在 这里 投我一票, 或者
- 设置我 为代理.
谢谢您! 我的贡献:SteemIt 工具、API接口、机器人和教程
股东工具
请注意:每次代理都是以最后一次输入的SP数量为标准,比如已经代理10 SP,想多代理5 SP则需要输入 最后的数字 15 SP(而不是 5!)
This page is synchronized from the post: ‘Coding Exercise - Find Letter Case Permutation with DFS 编程练习题:找出字符串的所有大小小组合’