Valid Word Abbreviation Leetcode

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

A string such as "word" contains only the following valid abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".

Note:
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.

Example 1:

Given s = "internationalization", abbr = "i12iz4n":

Return true.

Example 2:

Given s = "apple", abbr = "a2e":

Return false.

反正每次自己七改八改然后看top solution就会觉得自己逻辑混乱。。。= =

以下是我写的代码,有点难懂。。。

public class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int index1 = 0;
        int index2 = 0;
        char[] a = word.toCharArray();
        char[] b = abbr.toCharArray();
        while (index1 < a.length && index2 < b.length) {
            if (a[index1] != b[index2]) {
                int num = 0;
                if (index2 < b.length && b[index2] - ‘0‘ <= 0 || b[index2] - ‘0‘ > 9) {
                    return false;
                }
                while (index2 < b.length && b[index2] - ‘0‘ >= 0 && b[index2] - ‘0‘ <= 9) {
                    num = num * 10 + b[index2] - ‘0‘;
                    index2++;
                }
                if (num == 0) {
                    return false;
                } else {
                    int count = 0;
                    while (count < num) {
                        if (count < num && index1 > a.length - 1) {
                            return false;
                        }
                        index1++;
                        count++;
                    }
                }
            } else {
                index1++;
                index2++;
            }

        }
        return index1 == a.length && index2  == b.length;
    }
}

看了下top solution重新写的

public class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int index1 = 0;
        int index2 = 0;
        char[] a = word.toCharArray();
        char[] b = abbr.toCharArray();
        while (index1 < a.length && index2 < b.length) {
            if (a[index1] == b[index2]) {
                index1++;
                index2++;
                continue;
            }
            if (b[index2] - ‘0‘ <= 0 || b[index2] - ‘0‘ > 9) {
                System.out.println(1);
                return false;
            }
            int num = 0;
            while (index2 < b.length && b[index2] - ‘0‘ >= 0 && b[index2] - ‘0‘ <= 9) {
                num = num * 10 + b[index2] - ‘0‘;
                index2++;
            }
            index1 = index1 + num;
            if (index1 < a.length && index2 < b.length && a[index1] != b[index2]) {
                return false;
            }
        }
        return index1 == a.length && index2  == b.length;
    }
}

其中

if (index1 < a.length && index2 < b.length && a[index1] != b[index2]) {
                return false;
            }

这里可以省略的。

整体思路就是如果一样,就continue,不一样就看第一个数字是不是0,是0就false。然后把数字取出来,给index1加上,看看最后两个是不是都恰好把数组遍历了一遍。

反正这道题到时候需要回顾的。。。不是这里错就是那里错。

时间: 2024-10-26 21:21:51

Valid Word Abbreviation Leetcode的相关文章

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

LeetCode Valid Word Abbreviation

原题链接在这里:https://leetcode.com/problems/valid-word-abbreviation/#/description 题目: Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the followi

408. Valid Word Abbreviation

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

leetcode408 - Valid Word Abbreviation - easy

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

Unique Word Abbreviation -- LeetCode

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

408. Valid Word Abbreviation --字符串处理

Given s = "internationalization", abbr = "i12iz4n": Return true. abbr 里数字代表相应的字符数,问字符串是否相等 虽然是一个easy 的题但却有两个坑:1. abbr 结尾的地方是数字 例如: s= "internationalization" abbr= "i5a11o1" , 因此 return时得加上cout 来判断 index + Integer.va

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact

LeetCode Valid Word Square

原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(

[LeetCode] Valid Word Square 验证单词平方

Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤k < max(numRows, numColumns). Note: The number of words given is at le