2019/10/31【实现strStr()】&【串联所有单词的子串】

题目一:【实现strStr()】力扣——28

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

#!/bin/bash
#实现strStr()
#author:yzt 2019-10-31
#
content=`echo "$1"|grep "$2"`
if [ -z $content ];then
        echo "-1"
        exit 0
fi
count1=`echo "$1"|awk -F "$2" '{print NF}'`
str=$1
declare -i aa=0
while :
do
        str=${str:1}
        count2=`echo "$str"|awk -F "$2" '{print NF}'`
        if [ ! `echo $str|grep "$2"` ];then
                echo "$aa"
                exit 0
        fi
        if [ $count1 -ne $count2 ];then
                echo "$aa"
                exit 0
        fi
        aa=$[$aa+1]
done

 脚本逻辑:

1、第一通过grep可知长字符串中是否有短字符串,若没有则放回-1并退出程序

2、通过循环,在左边每次减少一个字符,需要使用一个变量记录已经减少了多少个字符;并用grep命令再次匹配,若没有则返回记录变量即可;

3、第二种方法有漏洞,就是当长字符串有多处与短字符串匹配时,记录就不准确了。此时,笔者考虑换一种方式,使用awk并使用短字符作为分隔符,获取分割后的列数;若列数减少,则返回记录变量。

 

脚本效果:

[root@localhost leetcode]# ./shixianstrStr.sh "hello" "ll"
2
[root@localhost leetcode]# ./shixianstrStr.sh "llhello" "ll"
0
[root@localhost leetcode]# ./shixianstrStr.sh "llhello" "lo"
5

 

 

题目二:【串联所有单词的子串】

给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

 

示例 1:

输入:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:

输入:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
输出:[]

#!/bin/bash
#串联所有单词的字串
#author:yzt 2019-10-31
#
result=""
cat /dev/null >tmp1.txt
read -t 20 -p "请输入字符串s的值:" s
read -t 20 -p "请输入words = :" words
length1=`echo "$words"|sed 's#,##g'|wc -L`
length2=`echo "$words"|awk -F , '{print $1}'|wc -L`
content1=`echo "$words"|sed 's#,#\n#g'|sort`
declare -i aa=1
while :
do
        bb=$[$aa+$length1-1]
        str1=`echo "$s"|cut -c "$aa-$bb"`
        length3=`echo "$str1"|wc -L`
        if [ $length1 -gt $length3 ];then
                break
        fi
        declare -i cc=1
        dd=$[$length2-1]
        cat /dev/null > tmp1.txt
        while :
        do
                ee=$[$cc+$dd]
                str2=`echo "$str1"|cut -c "$cc-$ee"`
                if [ -z $str2 ];then
                        break
                else
                        echo "$str2" >>tmp1.txt
                fi
                cc=$[$cc+$length2]
        done
        content2=`cat tmp1.txt|sort`
        if [ "$content1" = "$content2" ];then
                z=$[$aa-1]
                result=$result,$z
        fi
        aa=$[$aa+1]
done
xx=`echo "$result"|sed 's#^,##'`
echo "[$xx]"

 

脚本逻辑:

1、获取words的所有字符,因为每一个字符都是等长的,通过sort排序确定了word的唯一性;

2、获取words字符串的总长度length1和获取words中每个短字符的长度length2

3、使用循环每次在长字符串s中获取长度为length1的字符串str1,并在每次循环中s长字符串从左边开始减一位;在str1中截取长度为length2的短字符串并存储在文档file1中,将文档file1中的短字符串进行排序后赋值给变量content

4、通过比较变量content与word的唯一性可得知是否为目标索引值

脚本效果:

[root@localhost leetcode]# ./chuanliansuoyoudancidezichuan.sh
请输入字符串s的值:ddcasdfagddcdfsdgcddsdf
请输入words = :c,d,d
[0,9,10,17]
[root@localhost leetcode]# ./chuanliansuoyoudancidezichuan.sh
请输入字符串s的值:wordgoodgoodgoodbestword
请输入words = :word,good,best,word
[]
[root@localhost leetcode]# ./chuanliansuoyoudancidezichuan.sh
请输入字符串s的值:barfoothefoobarman
请输入words = :foo,bar
[0,9]

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值