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.

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

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

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

把字符串里的字母任意改为大写或小写,求能获得的所有字符。

我的解决思路是一个个遍历字符,碰到数字直接加上,碰的字母的,就把已经有的字符串复制一遍,前面一半字符串尾补加小写字母,后面一半字符串尾补加大写字母。

class Solution {
public:
    vector<string> letterCasePermutation(string S) {
        vector<string> res(1);
        for (int i=0; i<S.length(); ++i) {
            char c = S[i];
            int len = res.size();
            if ((c>=‘a‘ && c <=‘z‘) || (c>=‘A‘ && c<=‘Z‘)) {
                for (int j=0; j<len; ++j) {
                    res.push_back(res[j]);
                    res[j].push_back(tolower(c));
                    res[j+len].push_back(toupper(c));
                }
            }
            else
                for (int j=0; j<len; ++j)
                    res[j].push_back(c);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/Zzz-y/p/8457433.html

时间: 2024-10-17 00:06:30

784. Letter Case Permutation的相关文章

784. Letter Case Permutation - Easy

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",

LeetCode题解之 Letter Case Permutation

1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 1 vector<string> letterCasePermutation(string S) { 2 vector<string> res ; 3 recursion( S,res,0 ); 4 return res; 5 6 } 7 8 void recursion(string & s , vector<string> &r ,int p){ 9 if(

784. Letter Case Permutation---back tracking

题意: 字母变成大小写,数字不变Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345&

C# ignoring letter case for if statement(Stackoverflow)

Question: I have this if statement: if (input == 'day')     Console.Write({0}, dayData); When the user types 'day' it should be so that the console writes the data in that array. It works fine but is there anyway to get it to work if the user types '

LeetCode-784

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. Examples: Input: S = "a1b2" Output: ["a

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

Golang 特性简介

by sheepbao 主要大概介绍go语言的历史和特性,简单的入门. 来历 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛,正在用c++开发一些比较繁琐但是核心的工作,主要包括庞大的分布式集群,大牛觉得很闹心,后来c++委员会来他们公司演讲,说c++将要添加大概35种新特性.这几个大牛的其中一个人,名为:Rob Pike,听后心中一万个xxx飘过,“c++特性还不够多吗?简化c++应该更有成就感吧”.于是乎,Rob Pi

Codeforces VK Cup 2015 Wild Card Round 1 (AB)

比赛链接:http://codeforces.com/contest/522 A. Reposts time limit per test:1 second memory limit per test:256 megabytes One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started