A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
For example, a string such as "substitution "
could be abbreviated as (but not limited to):
"s10n "
( "s ubstitutio n "
)"sub4u4 "
( "sub stit u tion "
)"12 "
( "substitution "
)"su3i1u2on "
( "su bst i t u ti on "
)"substitution "
(no substrings replaced)The following are not valid abbreviations:
"s55n "
( "s ubsti tutio n "
, the replaced substrings are adjacent)"s010n "
(has leading zeros)"s0ubstitution "
(replaces an empty substring)Given a string word
and an abbreviation abbr
, return whether the string matches the given abbreviation.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: word = "internationalization ", abbr = "i12iz4n " Output: true Explanation: The word "internationalization " can be abbreviated as "i12iz4n " ( "i nternational iz atio n ").
Example 2:
Input: word = "apple ", abbr = "a2e " Output: false Explanation: The word "apple " cannot be abbreviated as "a2e ".
Constraints:
1 <= word.length <= 20
word
consists of only lowercase English letters.1 <= abbr.length <= 10
abbr
consists of lowercase English letters and digits.abbr
will fit in a 32-bit integer.program main
implicit none
character(len=*), parameter :: word = "substitution "
character(len=*), parameter :: abbr = "s10n "
logical :: matches
write(*,*) 'word: ', trim(word)
write(*,*) 'abbr: ', trim(abbr)
write(*,*) 'matches: ', matches(word, abbr)
contains
function matches(str, abbr) result(res)
implicit none
character(len=*), intent(in) :: str, abbr
logical :: res
integer :: i, j, k, l
res = .false.
i = 1
do while (i <= len_trim(str))
j = index(str(i:), ' ')
if (j == 0) then
exit
end if
k = index(abbr, ' ')
if (k /= 0) then
! there is a space in the abbreviation
if (j > k) then
! the substring before the space in the abbreviation is longer than the actual substring
exit
else
! the substring before the space in the abbreviation is shorter or equal to the actual substring
i = i + j - 1
cycle
end if
else
! no space in the abbreviation
if (j > len_trim(abbr)) then
! the actual substring is longer than the abbreviation
exit
else
! the actual substring is shorter or equal to the abbreviation
i = i + j - 1
cycle
end if
end if
l = len_trim(str(i:i+j-1))
if (l > 0) then
! the actual substring is not empty
if (index(abbr, l) == 0) then
! the length of the actual substring is not found in the abbreviation
exit
else
! the length of the actual substring is found in the abbreviation
i = i + j
cycle
end if
else
! the actual substring is empty
exit
end if
end do
if (i == len_trim(str)+1) then
! we reached the end of the string without finding a mismatch
res = .true.
end if
end function matches
end program main
temp.f95:14:21: 14 | function matches(str, abbr) result(res) | 1 Error: Procedure βmatchesβ at (1) has an explicit interface from a previous declaration temp.f95:15:21: 15 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:16:49: 16 | character(len=*), intent(in) :: str, abbr | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:22: 17 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:18:29: 18 | integer :: i, j, k, l | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:21: 20 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:13: 21 | i = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:37: 22 | do while (i <= len_trim(str)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:23:28: 23 | j = index(str(i:), ' ') | 1 Error: Syntax error in argument list at (1) temp.f95:24:28: 24 | if (j == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:20: 25 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:26:15: 26 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:27:32: 27 | k = index(abbr, ' ') | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:28: 28 | if (k /= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:30:31: 30 | if (j > k) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:24: 32 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:33:20: 33 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:35:33: 35 | i = i + j - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:36:25: 36 | cycle | 1 Error: CYCLE statement at (1) is not within a construct temp.f95:37:19: 37 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:16: 38 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:40:44: 40 | if (j > len_trim(abbr)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:42:24: 42 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:43:20: 43 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:45:33: 45 | i = i + j - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:46:25: 46 | cycle | 1 Error: CYCLE statement at (1) is not within a construct temp.f95:47:19: 47 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:48:15: 48 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:49:31: 49 | l = len_trim(str(i:i+j-1)) | 1 Error: Syntax error in argument list at (1) temp.f95:50:27: 50 | if (l > 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:52:45: 52 | if (index(abbr, l) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:54:24: 54 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:55:20: 55 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:57:29: 57 | i = i + j | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:58:25: 58 | cycle | 1 Error: CYCLE statement at (1) is not within a construct temp.f95:59:19: 59 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:60:16: 60 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:62:20: 62 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:63:15: 63 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:64:11: 64 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:65:38: 65 | if (i == len_trim(str)+1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:67:24: 67 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:68:11: 68 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:69:7: 69 | end function matches | 1 Error: Expecting END PROGRAM statement at (1)
program main
implicit none
integer :: i, j, k, l
character(len=20) :: word, abbr
logical :: match
! test case 1
word = "internationalization "
abbr = "i12iz4n "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 1: PASS"
else
print *, "Test case 1: FAIL"
end if
! test case 2
word = "apple "
abbr = "a2e "
match = match_abbr(word, abbr)
if (.not. match) then
print *, "Test case 2: PASS"
else
print *, "Test case 2: FAIL"
end if
! test case 3
word = "substitution "
abbr = "s10n "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 3: PASS"
else
print *, "Test case 3: FAIL"
end if
! test case 4
word = "substitution "
abbr = "sub4u4 "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 4: PASS"
else
print *, "Test case 4: FAIL"
end if
! test case 5
word = "substitution "
abbr = "12 "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 5: PASS"
else
print *, "Test case 5: FAIL"
end if
! test case 6
word = "substitution "
abbr = "su3i1u2on "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 6: PASS"
else
print *, "Test case 6: FAIL"
end if
! test case 7
word = "substitution "
abbr = "substitution "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 7: PASS"
else
print *, "Test case 7: FAIL"
end if
! test case 8
word = "substitution "
abbr = "s55n "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 8: PASS"
else
print *, "Test case 8: FAIL"
end if
! test case 9
word = "substitution "
abbr = "s010n "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 9: PASS"
else
print *, "Test case 9: FAIL"
end if
! test case 10
word = "substitution "
abbr = "s0ubstitution "
match = match_abbr(word, abbr)
if (match) then
print *, "Test case 10: PASS"
else
print *, "Test case 10: FAIL"
end if
contains
function match_abbr(word, abbr) result(match)
implicit none
character(len=*), intent(in) :: word, abbr
logical :: match
integer :: i, j, k, l
match = .false.
i = 1
j = 1
k = 1
l = 1
do while (i <= len(word) .and. j <= len(abbr))
if (abbr(j:j) == '0') then
j = j + 1
cycle
end if
if (
temp.f95:126:12: 126 | if ( | 1 Error: Invalid character in name at (1) f951: Error: Unexpected end of file in βtemp.f95β
def validWordAbbreviation(word, abbr):
i, j = 0, 0
while i < len(word) and j < len(abbr):
if abbr[j].isdigit():
if abbr[j] == '0': return False # leading zero
num = 0
while j < len(abbr) and abbr[j].isdigit():
num = num * 10 + int(abbr[j])
j += 1
i += num
else:
if word[i] != abbr[j]: return False
i, j = i + 1, j + 1
return i == len(word) and j == len(abbr)
The algorithm uses two pointers, i and j, to compare the characters in the word and the abbr. If the current character in abbr is a digit, we check if it's a leading zero and return false if so. Then, we sum all the consecutive digits to calculate the number and update i accordingly. If the current character is a letter, we compare the characters in word and abbr and return false if they're not equal. Finally, we check if we've reached the end of both the word and the abbr and return true if so.
bool validWordAbbreviation(string word, string abbr) {
int i = 0, j = 0;
while (i < word.size() && j < abbr.size()) {
if (isdigit(abbr[j])) {
if (abbr[j] == '0') return false; // leading zero
int num = 0;
while (j < abbr.size() && isdigit(abbr[j]))
num = num * 10 + (abbr[j++] - '0');
i += num;
} else {
if (word[i++] != abbr[j++]) return false;
}
}
return i == word.size() && j == abbr.size();
}
The algorithm uses two pointers, i and j, to compare the characters in the word and the abbr. If the current character in abbr is a digit, we check if it's a leading zero and return false if so. Then, we sum all the consecutive digits to calculate the number and update i accordingly. If the current character is a letter, we compare the characters in word and abbr and return false if they're not equal. Finally, we check if we've reached the end of both the word and the abbr and return true if so.