查找给定句子中按字典顺序递增和按字典顺序递减的所有单词
给定一个字符串,表示一个大小为N的名为str的句子,任务是找到一个句子中的所有有效单词,这些单词按字典顺序按升序和降序排序以及它们的计数。
Note: Valid words are:-
- words don’t containing numbers.
- words greater than size 1.
例子:
Input: str=”geeks for geeks”
Output:
1 “for”
0
Explanation: “for” is word which is lexicographically sorted in increasing order(1). There is no word which lexicographically sorted in decreasing order(0).
Input: str=”We won the match by 4 runs”
Output:
1 “by”
3 “we”, “won”, “the”
Explanation: “by” is sorted in increasing order(1) whereas “we”, “won” and “the” are lexicographically sorted in decreasing order(3).
方法:逐字遍历字符串并检查每个单词是按字典顺序增加还是按字典顺序减少的想法。根据找到的类型打印每个单词。
请按照以下步骤详细了解该方法:
- 首先遍历给定字符串中的单词,分别在count1和count2中计算按字典顺序递增和按字典顺序递减的单词数。
- 现在首先打印存储在 count1 中的按字典顺序递增的单词数
- 然后逐字再次遍历字符串并打印所有按字典顺序增加的单词。
- 对按字典顺序递减的单词重复最后两个步骤。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if string is
// sorted in increasing order
bool issorted(string s)
{
// using transform() function and ::tolower in STL
transform(s.begin(), s.end(), s.begin(), ::tolower);
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] > s[i + 1] or isdigit(s[i])) {
return false;
}
}
return true;
}
// Function to check ifstring is
// sorted in decreasing order
bool issortedre(string s)
{
// using transform() function and ::tolower in STL
transform(s.begin(), s.end(), s.begin(), ::tolower);
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] < s[i + 1] or isdigit(s[i])) {
return false;
}
}
return true;
}
// Function to count
// the required absolute difference
void count(string s)
{
stringstream ss(s);
string word;
int count1 = 0, count2 = 0;
// Checking validity of word
while (ss >> word) {
if (word.size() == 1)
continue;
// Counting the valid words
// in increasing order
if (issorted(word)) {
count1++;
}
// Counting the valid words
// in decreasing order
else if (issortedre(word)) {
count2++;
}
}
stringstream ss1(s);
// Printing count of
// lexicographically increasing words
cout << count1 << " ";
// Print all lexicographically
// increasing words
while (ss1 >> word) {
if (word.size() == 1)
continue;
// Printing all valid words
// in increasing order
if (issorted(word)) {
cout << "\"" << word << "\" ";
}
}
cout << endl;
stringstream ss2(s);
// Printing count of
// lexicographically increasing words
cout << count2 << " ";
// Print all lexicographically
// increasing words
while (ss2 >> word) {
if (word.size() == 1)
continue;
// Printing all valid words
// in decreasing order
else if (issortedre(word)) {
cout << "\"" << word << "\" ";
}
}
cout << endl;
}
// Driver Code
int main()
{
string s = "We won the match by 4 runs";
count(s);
return 0;
}
Java
// Java program for the above approach
class GFG {
static boolean isdigit(char c) {
return c >= '0' && c <= '9';
}
// Function to check if String is
// sorted in increasing order
public static boolean issorted(String s)
{
// using transform() function and ::tolower in STL
s = s.toLowerCase();
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) > s.charAt(i + 1) || isdigit(s.charAt(i))) {
return false;
}
}
return true;
}
// Function to check ifString is
// sorted in decreasing order
public static boolean issortedre(String s)
{
// using transform() function and ::tolower in STL
s = s.toLowerCase();
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) < s.charAt(i + 1) || isdigit(s.charAt(i))) {
return false;
}
}
return true;
}
// Function to count
// the required absolute difference
public static void count(String s) {
String[] ss = s.split(" ");
String word;
int count1 = 0, count2 = 0;
// Checking validity of word
for (int i = 0; i < ss.length; i++) {
word = ss[i];
if (word.length() == 1)
continue;
// Counting the valid words
// in increasing order
if (issorted(word)) {
count1++;
}
// Counting the valid words
// in decreasing order
else if (issortedre(word)) {
count2++;
}
}
String[] ss1 = s.split(" ");
// Printing count of
// lexicographically increasing words
System.out.print(count1 + " ");
// Print all lexicographically
// increasing words
for (int i = 0; i < ss1.length; i++) {
word = ss1[i];
if (word.length() == 1)
continue;
// Printing all valid words
// in increasing order
if (issorted(word)) {
System.out.print("\"" + word + "\" ");
}
}
System.out.println();
String[] ss2 = s.split(" ");
// Printing count of
// lexicographically increasing words
System.out.print(count2 + " ");
// Print all lexicographically
// increasing words
for (int i = 0; i < ss2.length; i++) {
word = ss2[i];
if (word.length() == 1)
continue;
// Printing all valid words
// in decreasing order
else if (issortedre(word)) {
System.out.print("\"" + word + "\" ");
}
}
System.out.println();
}
// Driver Code
public static void main(String args[]) {
String s = "We won the match by 4 runs";
count(s);
}
}
// This code is contributed by gfgking.
Python3
# Python Program to implement
# the above approach
def isCharNumber(c):
return c >= '0' and c <= '9'
# Function to check if string is
# sorted in increasing order
def issorted(s):
# using transform() function and ::tolower in STL
s = s.lower()
for i in range(len(s) - 1):
if (ord(s[i]) > ord(s[i + 1]) or isCharNumber(s[i])):
return False
return True
# Function to check ifstring is
# sorted in decreasing order
def issortedre(s):
# using transform() function and ::tolower in STL
s = s.lower()
for i in range(len(s) - 1):
if (ord(s[i]) < ord(s[i + 1]) or isCharNumber(s[i])):
return False
return True
# Function to count
# the required absolute difference
def count(s):
word = ""
count1 = 0
count2 = 0
ss = s.split(" ")
# Checking validity of word
for i in range(len(ss)):
word = ss[i]
if (len(word) == 1):
continue
# Counting the valid words
# in increasing order
if (issorted(word)):
count1 += 1
# Counting the valid words
# in decreasing order
elif (issortedre(word)):
count2 += 1
# Printing count of
# lexicographically increasing words
print(count1, end=" ")
# Print all lexicographically
# increasing words
for i in range(len(ss)):
word = ss[i]
if (len(word) == 1):
continue
# Printing all valid words
# in increasing order
if (issorted(word)):
print(f" {word} ")
# Printing count of
# lexicographically increasing words
print(count2, end=" ")
# Print all lexicographically
# increasing words
for i in range(len(ss)):
word = ss[i]
if (len(word) == 1):
continue
# Printing all valid words
# in decreasing order
elif (issortedre(word)):
print(f" {word} ", end=" ")
# Driver Code
s = "We won the match by 4 runs"
count(s)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
static bool isdigit(char c)
{
return c >= '0' && c <= '9';
}
// Function to check if String is
// sorted in increasing order
public static bool issorted(String s)
{
// Using transform() function and ::tolower in STL
s = s.ToLower();
for(int i = 0; i < s.Length - 1; i++)
{
if (s[i] > s[i + 1] || isdigit(s[i]))
{
return false;
}
}
return true;
}
// Function to check ifString is
// sorted in decreasing order
public static bool issortedre(String s)
{
// Using transform() function and ::tolower in STL
s = s.ToLower();
for(int i = 0; i < s.Length - 1; i++)
{
if (s[i] < s[i + 1] || isdigit(s[i]))
{
return false;
}
}
return true;
}
// Function to count the
// required absolute difference
public static void count(String s)
{
String[] ss = s.Split(' ');
String word;
int count1 = 0, count2 = 0;
// Checking validity of word
for(int i = 0; i < ss.Length; i++)
{
word = ss[i];
if (word.Length == 1)
continue;
// Counting the valid words
// in increasing order
if (issorted(word))
{
count1++;
}
// Counting the valid words
// in decreasing order
else if (issortedre(word))
{
count2++;
}
}
String[] ss1 = s.Split(' ');
// Printing count of lexicographically
// increasing words
Console.Write(count1 + " ");
// Print all lexicographically
// increasing words
for(int i = 0; i < ss1.Length; i++)
{
word = ss1[i];
if (word.Length == 1)
continue;
// Printing all valid words
// in increasing order
if (issorted(word))
{
Console.Write("\"" + word + "\" ");
}
}
Console.WriteLine();
String[] ss2 = s.Split(' ');
// Printing count of lexicographically
// increasing words
Console.Write(count2 + " ");
// Print all lexicographically
// increasing words
for(int i = 0; i < ss2.Length; i++)
{
word = ss2[i];
if (word.Length == 1)
continue;
// Printing all valid words
// in decreasing order
else if (issortedre(word))
{
Console.Write("\"" + word + "\" ");
}
}
Console.WriteLine();
}
// Driver Code
public static void Main(String []args)
{
String s = "We won the match by 4 runs";
count(s);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
1 "by"
3 "We" "won" "the"
时间复杂度: O(N^2)
辅助空间: O(N)