面试题 17.05. Find Longest Subarray LCCI

You are given an array of strings words and a string chars.

A string is good if it can be formed by characters from chars (each character can only be used once).

Return the sum of lengths of all good strings in words.

Example 1:

Input: words = [“cat”,“bt”,“hat”,“tree”], chars = “atach”
Output: 6
Explanation:
The strings that can be formed are “cat” and “hat” so the answer is 3 + 3 = 6.
Example 2:

Input: words = [“hello”,“world”,“leetcode”], chars = “welldonehoneyr”
Output: 10
Explanation:
The strings that can be formed are “hello” and “world” so the answer is 5 + 5 = 10.

Note:

1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
All strings contain lowercase English letters only.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/*
1, conert letters to 1, nums to -1.
2, build up a hash map, from array.begin() to end(), use diff as key and index as value
3, iter the array, if at jth the key(key!=0) is equal to ith, means subarray (i,j] have same number of letters and nums.
if jth key==0, means subarray (0,j] is valid array.
*/

#include<iostream>
#include<vector>
#include<string>
#include<map>

using namespace std;

class Solution {
public:
    vector<string> findLongestSubarray(vector<string>& array) {
        vector<string> ret;

        map<int, int> m;
        map<int, int>::iterator iter_m;
        int diff = 0;
        int len = 0;
        int index = -1;
        for (int i = 0; i < array.size(); i++)
        {
            if ((array[i] >= "A" && array[i] <= "Z") || (array[i] >= "a" && array[i] <= "z"))
                diff++;
            else
                diff--;

            if (!diff)
            {
                if (i + 1 > len)
                {
                    len = i + 1;
                    index = 0;
                }
            }
            else
            {
                iter_m = m.find(diff);
                if (iter_m == m.end())
                    m.insert(make_pair(diff, i));
                else
                {
                    if (i - iter_m->second > len)
                    {
                        len = i - iter_m->second;
                        index = iter_m->second + 1;
                    }
                }
            }
        }

        ret.assign(array.begin() + index, array.begin() + index + len);
        return ret;
    }
};

int main()
{
    Solution solution;

    vector<string> s = { "42","10","O","t","y","p","g","B","96","H","5","v","P","52","25","96","b","L","Y","z","d","52","3","v","71","J","A","0","v","51","E","k","H","96","21","W","59","I","V","s","59","w","X","33","29","H","32","51","f","i","58","56","66","90","F","10","93","53","85","28","78","d","67","81","T","K","S","l","L","Z","j","5","R","b","44","R","h","B","30","63","z","75","60","m","61","a","5","S","Z","D","2","A","W","k","84","44","96","96","y","M" };
    vector<string> ret;
    ret = solution.findLongestSubarray(s);
    
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值