784. Letter Case Permutation(python+cpp)

题目:

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 between 1 and 12. S will consist only of letters or digits.

解释:
字符串中的大小写随便改,输出所有可能的值。
python解法中,可以用到笛卡尔积 itertools.product

在数学中,两个集合X和Y的笛卡尓乘积(Cartesian product),又称直积,表示为X ×
Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员。假设集合A={a,b},集合B={0,1,2},则两个集合的笛卡尔积为{(a,0),
(a,1), (a,2), (b,0), (b,1), (b, 2)}。

*是解包的意思
对于函数或者方法如product(A, B, C, D)如果A B C D在一个列表里ls=[A, B, C, D],如何把ls传给product?用*,即product(*ls)

Input:[i for i in itertools.product([[1,2],[3,4]])]
Output:[([1, 2],), ([3, 4],)]
Input: [i for i in itertools.product(*[[1,2],[3,4]])]
Output:[(1, 3), (1, 4), (2, 3), (2, 4)]

python代码:

class Solution(object):
    def letterCasePermutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        #for i in S 在最后只需要出现一次
        _alpha=[[i.lower(),i.upper()]  if i.isalpha() else i for i in S]
        return [''.join(i) for i in itertools.product(*_alpha)]  

其实这种题目的经典解法是dfs,其中转换大小写有一种奇技淫巧
C++中,A->65,a->97,异或表示不考虑进位的加法
65^32=97,97^32=65转换大小写用这种写法,简直震惊了)
经典题目:不使用其他空间,交换两个值,32就是97和65的异或值(大小写字母ASCII码之间的异或值)

a = a ^ b;
b = a ^ b; //a ^ b ^ b = a ^ 0 = a;
a = a ^ b;

python代码:
竟然比笛卡尔积解法速度快。

class Solution:
    def letterCasePermutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        def dfs(s,index,res):
            if index==len(s):
                res.append(''.join(s))
                return 
            #不改变大小写的情况
            dfs(s,index+1,res)
            #改变大小写的情况
            if(s[index].isalpha()):
                #c++可以,但是python不能直接异或不同类型
                s[index]=chr(ord(s[index])^(1<<5))
                dfs(s,index+1,res)
        res=[]
        #因为python不能改变string类型
        S=list(S)
        dfs(S,0,res)
        return res      

c++代码:

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
    void dfs(string &s,int index,vector<string> &res)
    {
        if (index==s.size())
        {
            res.push_back(s);
            return ;
        }
        dfs(s,index+1,res);
        if (isalpha(s[index]))
        {
            s[index]^=(1<<5);
            dfs(s,index+1,res);
        }
    }
public:
    vector<string> letterCasePermutation(string S) {
        vector<string> res;
        dfs(S,0,res);
        return res;
    }
};

总结:
python的笛卡尔积
使用异或实现大小写的转换。
python的string不能直接修改元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值