当前位置:网站首页>LeetCode 1638. Count the number of substrings with only one character difference
LeetCode 1638. Count the number of substrings with only one character difference
2022-07-06 16:43:00 【Daylight629】
1638. Count the number of substrings with only one character difference
Here are two strings s
and t
, Please find out s
The number of non empty substrings in , These substrings satisfy the substitution A different character in the future , yes t
The substring of a string . In other words , Please find s
and t
In a string just The number of substring pairs with only one character .
For example , "**compute**r"
and "**computa**tion"
The bold part is only one character different : 'e'
/'a'
, So this pair of substrings will add... To the answer 1 .
Please return the number of different substring pairs that meet the above conditions .
One Substring Is a continuous character in a string .
Example 1:
Input :s = "aba", t = "baba"
Output :6
explain : The following is the difference only 1 A character s and t Substring pairs of strings :
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
The bold parts represent s and t String selected substring .
Example 2:
Input :s = "ab", t = "bb"
Output :3
explain : The following is the difference only 1 A character s and t Substring pairs of strings :
("ab", "bb")
("ab", "bb")
("ab", "bb")
The bold parts represent s and t String selected substring .
Example 3:
Input :s = "a", t = "a"
Output :0
Example 4:
Input :s = "abe", t = "bbc"
Output :10
Tips :
1 <= s.length, t.length <= 100
s
andt
All contain only lower case letters .
Two 、 Method 1
violence
class Solution {
public int countSubstrings(String s, String t) {
int res = 0;
int m = s.length();
int n = t.length();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int d = 0;
for (int k = 0; i + k < m && j + k < n; k++) {
if (s.charAt(i + k) != t.charAt(j + k)) {
d++;
}
if (d > 1) {
break;
}
if (d == 1) {
res++;
}
}
}
}
return res;
}
}
Complexity analysis
Time complexity :O(mnmin(m,n)), among m and n They are strings s and t The length of .
Spatial complexity :O(1).
3、 ... and 、 Method 2
Optimize
class Solution {
public int countSubstrings(String s, String t) {
int m = s.length();
int n = t.length();
int res = 0;
for (int d = -m + 1; d < n; d++) {
int i = 0, j = 0;
if (d > 0) {
j = d;
} else {
i = -d;
}
int fij = 0;
int gij = 0;
for (; i < m && j < n; i++, j++) {
if (s.charAt(i) == t.charAt(j)) {
gij++;
} else {
fij = gij + 1;
gij = 0;
}
res += fij;
}
}
return res;
}
}
Complexity analysis
- Time complexity :O((m+n)min(m,n))=O(mn), among m and n They are strings s and t The length of .
- Spatial complexity :O(1).
边栏推荐
- Market trend report, technical innovation and market forecast of double-sided foam tape in China
- 图像处理一百题(1-10)
- Simple records of business system migration from Oracle to opengauss database
- Summary of game theory
- Li Kou: the 81st biweekly match
- Li Kou - 298th weekly match
- Sublime text code formatting operation
- 第7章 __consumer_offsets topic
- 我在字节跳动「修电影」
- Story of [Kun Jintong]: talk about Chinese character coding and common character sets
猜你喜欢
QT implementation window gradually disappears qpropertyanimation+ progress bar
Chapter 6 datanode
Spark independent cluster dynamic online and offline worker node
Simply try the new amp model of deepfacelab (deepfake)
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
Chapter 5 yarn resource scheduler
Solve the problem that intel12 generation core CPU single thread only runs on small cores
视频压缩编码和音频压缩编码基本原理
Raspberry pie 4B installation opencv3.4.0
随机推荐
Spark independent cluster dynamic online and offline worker node
第2章 HFDS的Shell操作
Kubernetes集群部署
QT implementation window gradually disappears qpropertyanimation+ progress bar
Useeffect, triggered when function components are mounted and unloaded
ffmpeg命令行使用
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
音视频开发面试题
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
Summary of game theory
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
SQL quick start
875. Leetcode, a banana lover
Problem - 1646C. Factorials and Powers of Two - Codeforces
LeetCode 1552. Magnetic force between two balls
Hbuilder x format shortcut key settings
MP4格式详解
第五章 Yarn资源调度器