Lexicographic rank of a string
Here given code implementation process.
/*
C program for
Lexicographic rank of a string
*/
#include <stdio.h>
#include <string.h>
// Returns the number of smallest element in right side of starting point
int findSmallerRight(char *text, int start, int last)
{
int count = 0;
for (int i = start + 1; i <= last; ++i)
{
if (text[i] < text[start])
{
// When location of i value is small of starting point
count++;
}
}
return count;
}
void findLexicographicRank(char *text)
{
int n = strlen(text);
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
int rank = 1;
int multiplier = 1;
int small = 0;
// Calculate the factorial of text length
for (int i = 1; i <= n; ++i)
{
multiplier *= i;
}
// Execute the loop through by size of text
for (int i = 0; i < n; ++i)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small *multiplier);
}
// Display given text
printf(" Text : %s", text);
// Display calculate rank
printf("\n Rank : %d\n", rank);
}
int main(int argc, char const *argv[])
{
// Given string text
char *text1 = "runcode";
char *text2 = "logic";
char *text3 = "count";
findLexicographicRank(text1);
findLexicographicRank(text2);
findLexicographicRank(text3);
return 0;
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
/*
Java program for
Lexicographic rank of a string
*/
public class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
public int findSmallerRight(String text, int start, int last)
{
int count = 0;
for (int i = start + 1; i <= last; ++i)
{
if (text.charAt(i) < text.charAt(start))
{
// When location of i value is small of starting point
count++;
}
}
return count;
}
public void findLexicographicRank(String text)
{
int n = text.length();
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
int rank = 1;
int multiplier = 1;
int small = 0;
// Calculate the factorial of text length
for (int i = 1; i <= n; ++i)
{
multiplier *= i;
}
// Execute the loop through by size of text
for (int i = 0; i < n; ++i)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
}
// Display given text
System.out.print("\n Text : " + text);
// Display calculate rank
System.out.println("\n Rank : " + rank);
}
public static void main(String[] args)
{
LexicographicRank task = new LexicographicRank();
// Given string text
String text1 = "runcode";
String text2 = "logic";
String text3 = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Lexicographic rank of a string
*/
class LexicographicRank
{
public:
// Returns the number of smallest element in right side of starting point
int findSmallerRight(string text, int start, int last)
{
int count = 0;
for (int i = start + 1; i <= last; ++i)
{
if (text[i] < text[start])
{
// When location of i value is small of starting point
count++;
}
}
return count;
}
void findLexicographicRank(string text)
{
int n = text.length();
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
int rank = 1;
int multiplier = 1;
int small = 0;
// Calculate the factorial of text length
for (int i = 1; i <= n; ++i)
{
multiplier *= i;
}
// Execute the loop through by size of text
for (int i = 0; i < n; ++i)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = this->findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small *multiplier);
}
// Display given text
cout << "\n Text : " << text;
// Display calculate rank
cout << "\n Rank : " << rank << endl;
}
};
int main()
{
LexicographicRank *task = new LexicographicRank();
// Given string text
string text1 = "runcode";
string text2 = "logic";
string text3 = "count";
task->findLexicographicRank(text1);
task->findLexicographicRank(text2);
task->findLexicographicRank(text3);
return 0;
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
// Include namespace system
using System;
/*
Csharp program for
Lexicographic rank of a string
*/
public class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
public int findSmallerRight(String text, int start, int last)
{
int count = 0;
for (int i = start + 1; i <= last; ++i)
{
if (text[i] < text[start])
{
// When location of i value is small of starting point
count++;
}
}
return count;
}
public void findLexicographicRank(String text)
{
int n = text.Length;
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
int rank = 1;
int multiplier = 1;
int small = 0;
// Calculate the factorial of text length
for (int i = 1; i <= n; ++i)
{
multiplier *= i;
}
// Execute the loop through by size of text
for (int i = 0; i < n; ++i)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = this.findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
}
// Display given text
Console.Write("\n Text : " + text);
// Display calculate rank
Console.WriteLine("\n Rank : " + rank);
}
public static void Main(String[] args)
{
LexicographicRank task = new LexicographicRank();
// Given string text
String text1 = "runcode";
String text2 = "logic";
String text3 = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
package main
import "fmt"
/*
Go program for
Lexicographic rank of a string
*/
// Returns the number of smallest element in right side of starting point
func findSmallerRight(text string, start int, last int) int {
var count int = 0
for i := start + 1 ; i <= last ; i++ {
if text[i] < text[start] {
// When location of i value is small of starting point
count++
}
}
return count
}
func findLexicographicRank(text string) {
var n int = len(text)
if n == 0 {
return
}
// Declare some useful auxiliary variable
var rank int = 1
var multiplier int = 1
var small int = 0
// Calculate the factorial of text length
for i := 1 ; i <= n ; i++ {
multiplier *= i
}
// Execute the loop through by size of text
for i := 0 ; i < n ; i++ {
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i)
// Count all smaller element in right side of i
small = findSmallerRight(text, i, n - 1)
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier)
}
// Display given text
fmt.Print("\n Text : ", text)
// Display calculate rank
fmt.Println("\n Rank : ", rank)
}
func main() {
// Given string text
var text1 string = "runcode"
var text2 string = "logic"
var text3 string = "count"
findLexicographicRank(text1)
findLexicographicRank(text2)
findLexicographicRank(text3)
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
<?php
/*
Php program for
Lexicographic rank of a string
*/
class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
public function findSmallerRight($text, $start, $last)
{
$count = 0;
for ($i = $start + 1; $i <= $last; ++$i)
{
if ($text[$i] < $text[$start])
{
// When location of i value is small of starting point
$count++;
}
}
return $count;
}
public function findLexicographicRank($text)
{
$n = strlen($text);
if ($n == 0)
{
return;
}
// Declare some useful auxiliary variable
$rank = 1;
$multiplier = 1;
$small = 0;
// Calculate the factorial of text length
for ($i = 1; $i <= $n; ++$i)
{
$multiplier *= $i;
}
// Execute the loop through by size of text
for ($i = 0; $i < $n; ++$i)
{
// Reduce multiplier by diving (n-i)
$multiplier = (int)($multiplier / ($n - $i));
// Count all smaller element in right side of i
$small = $this->findSmallerRight($text, $i, $n - 1);
// Calculate rank by (rank + (small *multiplier))
$rank = $rank + ($small * $multiplier);
}
// Display given text
echo("\n Text : ".$text);
// Display calculate rank
echo("\n Rank : ".$rank."\n");
}
}
function main()
{
$task = new LexicographicRank();
// Given string text
$text1 = "runcode";
$text2 = "logic";
$text3 = "count";
$task->findLexicographicRank($text1);
$task->findLexicographicRank($text2);
$task->findLexicographicRank($text3);
}
main();
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
/*
Node JS program for
Lexicographic rank of a string
*/
class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
findSmallerRight(text, start, last)
{
var count = 0;
for (var i = start + 1; i <= last; ++i)
{
if (text.charAt(i) < text.charAt(start))
{
// When location of i value is small of starting point
count++;
}
}
return count;
}
findLexicographicRank(text)
{
var n = text.length;
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
var rank = 1;
var multiplier = 1;
var small = 0;
// Calculate the factorial of text length
for (var i = 1; i <= n; ++i)
{
multiplier *= i;
}
// Execute the loop through by size of text
for (var i = 0; i < n; ++i)
{
// Reduce multiplier by diving (n-i)
multiplier = parseInt(multiplier / (n - i));
// Count all smaller element in right side of i
small = this.findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
}
// Display given text
process.stdout.write("\n Text : " + text);
// Display calculate rank
console.log("\n Rank : " + rank);
}
}
function main()
{
var task = new LexicographicRank();
// Given string text
var text1 = "runcode";
var text2 = "logic";
var text3 = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
main();
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
# Python 3 program for
# Lexicographic rank of a string
class LexicographicRank :
# Returns the number of smallest element in right side of starting point
def findSmallerRight(self, text, start, last) :
count = 0
i = start + 1
while (i <= last) :
if (text[i] < text[start]) :
# When location of i value is small of starting point
count += 1
i += 1
return count
def findLexicographicRank(self, text) :
n = len(text)
if (n == 0) :
return
# Declare some useful auxiliary variable
rank = 1
multiplier = 1
small = 0
i = 1
# Calculate the factorial of text length
while (i <= n) :
multiplier *= i
i += 1
i = 0
# Execute the loop through by size of text
while (i < n) :
# Reduce multiplier by diving (n-i)
multiplier = int(multiplier / (n - i))
# Count all smaller element in right side of i
small = self.findSmallerRight(text, i, n - 1)
# Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier)
i += 1
# Display given text
print("\n Text : ", text, end = "")
# Display calculate rank
print("\n Rank : ", rank)
def main() :
task = LexicographicRank()
# Given string text
text1 = "runcode"
text2 = "logic"
text3 = "count"
task.findLexicographicRank(text1)
task.findLexicographicRank(text2)
task.findLexicographicRank(text3)
if __name__ == "__main__": main()
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
# Ruby program for
# Lexicographic rank of a string
class LexicographicRank
# Returns the number of smallest element in right side of starting point
def findSmallerRight(text, start, last)
count = 0
i = start + 1
while (i <= last)
if (text[i] < text[start])
# When location of i value is small of starting point
count += 1
end
i += 1
end
return count
end
def findLexicographicRank(text)
n = text.length
if (n == 0)
return
end
# Declare some useful auxiliary variable
rank = 1
multiplier = 1
small = 0
i = 1
# Calculate the factorial of text length
while (i <= n)
multiplier *= i
i += 1
end
i = 0
# Execute the loop through by size of text
while (i < n)
# Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i)
# Count all smaller element in right side of i
small = self.findSmallerRight(text, i, n - 1)
# Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier)
i += 1
end
# Display given text
print("\n Text : ", text)
# Display calculate rank
print("\n Rank : ", rank, "\n")
end
end
def main()
task = LexicographicRank.new()
# Given string text
text1 = "runcode"
text2 = "logic"
text3 = "count"
task.findLexicographicRank(text1)
task.findLexicographicRank(text2)
task.findLexicographicRank(text3)
end
main()
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
/*
Scala program for
Lexicographic rank of a string
*/
class LexicographicRank()
{
// Returns the number of smallest element in right side of starting point
def findSmallerRight(
text: String,
start: Int,
last: Int): Int = {
var count: Int = 0;
var i: Int = start + 1;
while (i <= last)
{
if (text.charAt(i) < text.charAt(start))
{
// When location of i value is small of starting point
count += 1;
}
i += 1;
}
return count;
}
def findLexicographicRank(text: String): Unit = {
var n: Int = text.length();
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
var rank: Int = 1;
var multiplier: Int = 1;
var small: Int = 0;
var i: Int = 1;
// Calculate the factorial of text length
while (i <= n)
{
multiplier *= i;
i += 1;
}
i = 0;
// Execute the loop through by size of text
while (i < n)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
i += 1;
}
// Display given text
print("\n Text : " + text);
// Display calculate rank
println("\n Rank : " + rank);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: LexicographicRank = new LexicographicRank();
// Given string text
var text1: String = "runcode";
var text2: String = "logic";
var text3: String = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
import Foundation;
/*
Swift 4 program for
Lexicographic rank of a string
*/
class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
func findSmallerRight(_ text: [Character], _ start: Int, _ last: Int) -> Int
{
var count: Int = 0;
var i: Int = start + 1;
while (i <= last)
{
if (text[i] < text[start])
{
// When location of i value is small of starting point
count += 1;
}
i += 1;
}
return count;
}
func findLexicographicRank(_ data: String)
{
let text = Array(data);
let n: Int = text.count;
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
var rank: Int = 1;
var multiplier: Int = 1;
var small: Int = 0;
var i: Int = 1;
// Calculate the factorial of text length
while (i <= n)
{
multiplier *= i;
i += 1;
}
i = 0;
// Execute the loop through by size of text
while (i < n)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = self.findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
i += 1;
}
// Display given text
print("\n Text : ", data, terminator: "");
// Display calculate rank
print("\n Rank : ", rank);
}
}
func main()
{
let task: LexicographicRank = LexicographicRank();
// Given string text
let text1: String = "runcode";
let text2: String = "logic";
let text3: String = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
main();
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
/*
Kotlin program for
Lexicographic rank of a string
*/
class LexicographicRank
{
// Returns the number of smallest element in right side of starting point
fun findSmallerRight(text: String, start: Int, last: Int): Int
{
var count: Int = 0;
var i: Int = start + 1;
while (i <= last)
{
if (text.get(i) < text.get(start))
{
// When location of i value is small of starting point
count += 1;
}
i += 1;
}
return count;
}
fun findLexicographicRank(text: String): Unit
{
val n: Int = text.length;
if (n == 0)
{
return;
}
// Declare some useful auxiliary variable
var rank: Int = 1;
var multiplier: Int = 1;
var small: Int ;
var i: Int = 1;
// Calculate the factorial of text length
while (i <= n)
{
multiplier *= i;
i += 1;
}
i = 0;
// Execute the loop through by size of text
while (i < n)
{
// Reduce multiplier by diving (n-i)
multiplier = multiplier / (n - i);
// Count all smaller element in right side of i
small = this.findSmallerRight(text, i, n - 1);
// Calculate rank by (rank + (small *multiplier))
rank = rank + (small * multiplier);
i += 1;
}
// Display given text
print("\n Text : " + text);
// Display calculate rank
println("\n Rank : " + rank);
}
}
fun main(args: Array < String > ): Unit
{
val task: LexicographicRank = LexicographicRank();
// Given string text
val text1: String = "runcode";
val text2: String = "logic";
val text3: String = "count";
task.findLexicographicRank(text1);
task.findLexicographicRank(text2);
task.findLexicographicRank(text3);
}
Output
Text : runcode
Rank : 4277
Text : logic
Rank : 94
Text : count
Rank : 11
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment