给定一个空间分隔字符串str,任务是找到的第一个字符串,其反向是字符串中的一句话。字符串的所有字符都是小写英文字母。字符串以#结尾。如果不存在这样的子字符串,则返回-1
例子:
Input: str = “mango is sweet when nam en tastes it#”
Output: man
Explanation: Substring “man” is reversed to “nam” and it is a word in the given string
Input: str = “hello world#”
Output: -1
方法:
- 将字符串的所有单词存储在地图上。
- 找出字符串的所有子字符串。
- 对于每个子串,检查子的反向是否是字符串的一个字。如果不存在这样的子字符串,则打印 -1。
下面是上述方法的实现。
C++
// C++ program to find first substring
#include
using namespace std;
// Function to find first substring
string first_substring(string s)
{
int n = s.length(), c = 0;
string s1, s2;
map mpp;
// Storing the words present in string
for (int i = 0; i < n; i++) {
if (s[i] == ' ' || s[i] == '#') {
s1 = s.substr(c, i - c);
mpp[s1] = 1;
c = i + 1;
}
}
// Calculating for all
// possible valid substring.
for (int i = 0; i < n; i++) {
if (s[i] == ' ') {
continue;
}
for (int j = 0; j < n; j++) {
if (s[j] == ' ') {
break;
}
s1 = s.substr(i, j - i + 1);
s2 = s1;
reverse(s1.begin(), s1.end());
if (mpp[s1]) {
return s2;
}
}
}
return "-1";
}
// Driver code
int main()
{
string s, s1;
s = "mango is sweet when nam en tastes it#";
s1 = first_substring(s);
cout << s1 << "\n";
return 0;
}
Java
// Java program to find first subString
import java.util.*;
class GFG
{
// Function to find first subString
static String first_subString(String s)
{
int n = s.length(), c = 0;
String s1, s2;
HashMap mpp =
new HashMap();
// Storing the words present in String
for (int i = 0; i < n; i++)
{
if (s.charAt(i) == ' ' || s.charAt(i) == '#')
{
s1 = s.substring(c, i);
mpp.put(s1, 1);
c = i + 1;
}
}
// Calculating for all
// possible valid subString.
for (int i = 0; i < n; i++)
{
if (s.charAt(i) == ' ')
{
continue;
}
for (int j = 0; j < n; j++)
{
if (s.charAt(i) == ' ')
{
break;
}
s1 = s.substring(i, j - i + 1);
s2 = s1;
s1 = reverse(s1);
if (mpp.containsKey(s1))
{
return s2;
}
}
}
return "-1";
}
static String reverse(String input)
{
char[] a = input.toCharArray();
int l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
// Driver code
public static void main(String[] args)
{
String s, s1;
s = "mango is sweet when nam en tastes it#";
s1 = first_subString(s);
System.out.print(s1+ "\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find first substring
# Function to find first substring
def first_substring(s) :
n = len(s); c = 0;
mpp = {};
# Storing the words present in string
for i in range(n) :
if (s[i] == ' ' or s[i] == '#') :
s1 = s[c: i];
mpp[s1] = 1;
c = i + 1;
# Calculating for all
# possible valid substring.
for i in range(n) :
if (s[i] == ' ') :
continue;
for j in range(n) :
if (s[j] == ' ') :
break;
s1 = s[i : j + 1];
s2 = s1;
s1 = s1[::-1];
if s1 in mpp :
if mpp[s1] :
return s2;
return "-1";
# Driver code
if __name__ == "__main__" :
s = "mango is sweet when nam en tastes it#";
s1 = first_substring(s);
print(s1);
# This code is contributed by AnkitRai01
C#
// C# program to find first subString
using System;
using System.Collections.Generic;
class GFG
{
// Function to find first subString
static String first_subString(String s)
{
int n = s.Length, c = 0;
String s1, s2;
Dictionary mpp =
new Dictionary();
// Storing the words present in String
for (int i = 0; i < n; i++)
{
if (s[i] == ' ' || s[i] == '#')
{
s1 = s.Substring(c, i - c);
mpp.Add(s1, 1);
c = i + 1;
}
}
// Calculating for all
// possible valid subString.
for (int i = 0; i < n; i++)
{
if (s[i] == ' ')
{
continue;
}
for (int j = 0; j < n; j++)
{
if (s[i] == ' ')
{
break;
}
s1 = s.Substring(i, j - i + 1);
s2 = s1;
s1 = reverse(s1);
if (mpp.ContainsKey(s1))
{
return s2;
}
}
}
return "-1";
}
static String reverse(String input)
{
char[] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join("", a);
}
// Driver code
public static void Main(String[] args)
{
String s, s1;
s = "mango is sweet when nam en tastes it#";
s1 = first_subString(s);
Console.Write(s1 + "\n");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
man
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。